Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VFP9 SP1: listbox doesn't accept array as rowsourcetype

Status
Not open for further replies.

Eliott

Programmer
Nov 8, 2009
91
BA
Hi, until now I never used listbox control with RowSourceType=5 (array). I made some form with a commandbutton and listbox based on this code inside button click() event:
Code:
dimension mycolors[4]
colors(1)="black"
colors(2)="white"
colors(3)="red"
colors(4)="green"
thisform.list1.RowSourceType= 5
thisform.list1.RowSource= "mycolors"

but nothing appears! Background of Listbox appears like something invisible populated it but after click there is nothing! If I click fast few time then sometime appears items but when I click on Listbox they disapears!?
I saw also example on M$ MSDN page about SQL result into Thisform.myarray but also doesn't work for me! Is there some trick like some of them exist for grid object or just it's up to me? Thank you.

There is no good nor evil, just decisions and consequences.
 
The problem is scope. I don't know where you're declaring that array, but no doubt it's not in scope when the list needs it.

The best way to use an array as a RowSource is to add a custom array property to your listbox class and make that the RowSource. If you're not using a ListBox subclass, so can't do that, the next best thing is to make the array a property of the form.

If the array is a List property, refer to it as This.MyArray in RowSource and in code. If it's a form property, refer to to it as ThisForm.MyArray.

Tamar
 
Hi,

I believe the problem is a false declaration of your array.
Please change as flws:

Code:
dimension myColors[4]
myColors(1)="black"
myColors(2)="white"
myColors(3)="red"
myColors(4)="green"
thisform.list1.RowSourceType= 5
thisform.list1.RowSource= "myColors"


Regards,

Jockey(2)
 
Hi, not problem with name of arrays in Rowsource part. Simply listbox control doesn't "bite" such plain definition. But thanks for your help.

There is no good nor evil, just decisions and consequences.
 
Well, I think I found some kind of solution or middle-solution. I made a command button where is:
Code:
DIMENSION MyColors(4)
MyColors(1)="black"
MyColors(2)="white"
MyColors(3)="red"
MyColors(4)="green"
*
thisform.list1.RowSourceType= 5
thisform.list1.RowSource= 'MyColors'
thisform.list1.visible=.t.
READ EVENTS  && Start event processing

but form won't close itself with Thisform.Release in command button reserved for close form until I put there: Clear Events before. In this moment I'm not sure will this Read Events harm previous form code whit navigation buttons etc. objects... but good news is that this code works. Anybody has different idea?

There is no good nor evil, just decisions and consequences.
 
No, that code most definitely does NOT work! READ EVENTS makes sure the method never ends so the array does not go out of scope, as Tamar described. She's exactly correct, with one small omission.

If you've properly scoped the array (hint: from what I've seen you haven't), and the listbox doesn't display the array after you've changed the RowSource, then you need to call listbox.Requery() to get the list to read the new RowSource.

Your application should have only one READ EVENTS (and in fact CAN only have one), and it doesn't belong buried in a button's click method.
 
You just need to listen to what Tamar said in the first place. The array is gone when the method ends, you need a persitant array, if you want the listbox to diplay it.

Read about variable scope in the help.

A way to make the array last as long as the listbox is there is add it to the listbox:

Code:
With thisform.list1
.Addproperty("MyColors[4]")
.MyColors(1)="black"
.MyColors(2)="white"
.MyColors(3)="red"
.MyColors(4)="green"

.RowSourceType= 5
.RowSource= 'This.MyColors'
.visible=.t.
Endwith

Besides, in this case why not simpler use

Code:
With thisform.list1
.RowSourceType= 1
.RowSource= 'black,white,red,green'
.visible=.t.
Endwith

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top