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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with listbox using multiple arrays for a source. 1

Status
Not open for further replies.

iyarosh

Programmer
Apr 14, 2003
49
US
Hello,

I have a form with three buttons and listbox (vendorBox). When either button is pressed I create a new array from select statement and use following statements to specify the source: THISFORM.vendorBox.RowSourceType = 5 and
THISFORM.vendorBox.RowSource = 'vendorList'. When I run the form it looks like the listbox is populated, but the result is not visible. To resolve it, at the beginning of click event for each button I put clear events statement with matching read events at the end. Now, the code works but I have to click each button twice to get the list displayed. (if clicked once read events statement is skipped and the execution jumps to the click event from another button. Second click puts the pointer to the proper read statement and it works.) It would not be a problem if I did not have to create a select statement when form is released. THere are two issues. First, the form does not get released until the entire code is executed and, second, if I press any button on the form while the code is executed my dynamic select statement keeps growing, cause the execution goes through the destroy event every time form is touched. I've tried almost all suggestions found on this forum, but did not get the problem resolved. I do not have any problems with a listbox being populated from a single array, but it does not work for multiple sources. If anyone knows how to make the listbox work from a single click please help. Any reply is highly appreciated. Thank you!
 
First, you don't need a READ EVENTS or CLEAR EVENTS while you're developing the form - running fromm within the VFP IDE. VFP itself handles that.
You will only need one READ EVENTS when you build the executable, and you will typically placed that statement in the main .prg which calls the form. Clear events is typically placed in the Release method of the form.

Now each time you rebuild the array and want to repoulate the list, you will need to set the source off first. For example, if the row source is 'vendorList', try this:
Code:
THISFORM.vendorBox.RowSource = ''
*... code to build array
THISFORM.vendorBox.RowSource = 'vendorList'
THISFORM.vendorBox.Refresh

-Dave Summers-
[cheers]
Even more Fox stuff at:
 
Dave,

Thank you for your reply. Unfortunately, it did not work. It does populate the box (I see the right number of records being pulled from array and I can select the row, and scroll bar appears), but the listbox itself is invisible. The reason I put read and clear events at the beginning and end of every click event is because it does not work without these statements. I know it's wrong, but can not figure out what exactly needs to be done. Is there anything else I could try??? Thank you!

Igor.
 
Since the listbox has a RowSource of Array, you need to call the Listbox.Requery(). The Listbox.Requery() loads the List from the array. It is not enough to repopulate the array. So first fill in the array with the appropriate data, then call the Listbox.Requery().


_RAS
VFP MVP
 
Dave,

I guess it would be easier if you saw the code. Just for clarify sake. When 1st button is clicked for the first time the code works fine. When the secod one is clicked, the code gets executed through the button 2 logic skipping read events, then it jumps to read events statement on button #3, then exits out leaving the text box invisible. If the second button is pressed again, it would work properly. In case there is no solution to this problem I was wondering if there is a way to force the program to stop at the read events statement? It seems to be working fine (via debugger) until the execution encounters read events and the list box becomes invisible/empty. The code for all three buttons is here:

***Button 1 click event
Code:
clear events

vendorCount=0
thisform.vendorsSelected.refresh(vendorCount)

thisform.unselectAll.click
release vendorList
THISFORM.vendorBox.RowSource = ''

select vendor+' - '+name	;
  from vendorta 				;
where !empty(POCOUNTRY) 	;
    and !empty(vendor) 		;
    and vendor<>'ZZZ' 		;
 order by vendor 			;
   into array vendorList

THISFORM.vendorBox.RowSourceType = 5
THISFORM.vendorBox.RowSource = 'vendorList'
THISFORM.vendorBox.Refresh

read events

*** Button 2 click event
Code:
* Select Domestic Vendors only:
clear events
*set step on
vendorCount=0
thisform.vendorsSelected.refresh(vendorCount)

thisform.unselectAll.click
release vendorList
THISFORM.vendorBox.RowSource = ''

select vendor+' - '+name	;
  from vendorta 			;
 where empty(POCOUNTRY) 	;
   and !empty(vendor) 		;
   and vendor<>'ZZZ' 		;
 order by vendor 			;
  into array vendorList

THISFORM.vendorBox.RowSourceType = 5
THISFORM.vendorBox.RowSource = 'vendorList'
THISFORM.vendorBox.Refresh
read events

***Button 3 click event
Code:
clear events
*set step on
vendorCount=0
thisform.vendorsSelected.refresh(vendorCount)

thisform.unselectAll.click
release vendorList
THISFORM.vendorBox.RowSource = ''

select vendor+' - '+name	;
  from vendorta 			;
 where !empty(vendor) 		;
   and vendor<>'ZZZ' 		;
 order by vendor 			;
  into array vendorList

THISFORM.vendorBox.RowSourceType = 5
THISFORM.vendorBox.RowSource = 'vendorList'
THISFORM.vendorBox.Refresh

read events

Thank you!
 
Rick,

It did not work either; I'm getting the same results as with no requery(). I've tried to run it with and without read/clear statements. Still pulling my hair out :)

Thank you,
Igor.
 
This is all the code you should need:

Code:
vendorCount=0
thisform.vendorsSelected.refresh(vendorCount)

thisform.unselectAll.click
*< RAS, not necessary, listboxes do not lose their
*< marbles like a grid does when you requery the data.
*< release vendorList
*< THISFORM.vendorBox.RowSource = ''

select vendor+' - '+name    ;
  from vendorta             ;
 where !empty(vendor)         ;
   and vendor<>'ZZZ'         ;
 order by vendor             ;
  into array vendorList

* The next two lines are only necessary if you do not
* set them in the property sheet.
THISFORM.vendorBox.RowSourceType = 5
THISFORM.vendorBox.RowSource = 'vendorList'

* This is the key line
THISFORM.vendorBox.Requery()


_RAS
VFP MVP
 
Rick,

Although it works differently now, it still does not show the list. When I changed the code for my first button it started working but only if I pressed second or third buttons; it would not work if I pressed it after the form is loaded. When I replased the code for all three buttons it stopped working completely. Something goes out of wack with read event statements. I run the program, which calls my main screen; this program has read events statement. From the main screen I press the button to launch the vendor screen. I've tried to call vendor screen from my program directly and still no luck. Tried putting read statement all over the places with the same result. Your suggestion definately made the change and I feel that it's a right direction, but still can not figure that out.

Thank you,
Igor.
 
You only need one READ EVENT in the main program after the form call.

The code I supplied is all the code you need for the one button. READ EVENT inserts a wait state into the application. If you include it in a button Click() then the wait state exists at this point in time, Clicking one of the other buttons executes the CLEAR EVENT which stops the wait state, makes the Listbox appear to work (from the previous button), then is doing another READ EVENTS which wait states again, before the Listbox can be updated. READ EVENTS is not a replacement for the FPD/FPW READ command.

The moral of this story is STOP inserting READ EVENTS to attempt to fix this problem. Remove all of them except the one in the main program or menu.



_RAS
VFP MVP
 
Rick,

I did it before and tried it now few more time, however getting the same issue with empty listbox. Double checked, took an exact copy from your example- no luck. The only read events statement I have is the one from the main program. The form acts as it's getting the data from array into the box, but nothing is visible. The visible property is set to true on the form. Is that possible there is something else is missing. I appreciate your help.

Thanks,
Igor.
 
Are you sure the queries are really returning records? I would suspend the program after the Listbox.Requery(), check in the debugger to make sure the array has the right data, then check the listbox's List to make sure it has the right data.

Another goofy question, is it possible the forecolor of the listbox is not black?

I'm grabbing at straws at this point.

_RAS
VFP MVP
 
I ran the code through the debugger and confirm that the query I was looking at returns the records, which are then passed into the array, which also seems to be ok. The status bar shows how many records were pulled out and the list box gets its data when THISFORM.vendorBox.RowSource = 'vendorList' gets executed and stays there when THISFORM.vendorBox.Requery()statement is ran, then when the control jumps back to read events from the main program the listbox gets cleared out. The forecolor of the list box is black. I also checked that the rest of the colers were black to where appropriate. No luck :(
 
Ah, it is a scoping issue. I should have picked up on this earlier.

The array goes out of scope when the method is finished. You need to create an array property on the form or the Listbox (which is where I have mine - aList[1]). Use this array as the RowSource and you should be golden.


_RAS
VFP MVP
 
Rick,

My hat goes down. Works like magic. Can't thank you enough.

Igor.
 
Thanks for the challenge Igor, glad to hear we finally solved this. I have been working on an object all afternoon to zip and unzip files and was wondering which problem I was going to solve first <g>.

(I finished mine first in case you are curious)


_RAS
VFP MVP
 
First things come first. I'm fairly new to OO programming and this particular issue was bugging me for a few days (my brain was refusing to think about it anymore). I'm very happy that there are people like you who can always give you a hand and not get frustrated through the process. Thanks again.

Igor.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top