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

VFP listbox deselects items when it loses focus 1

Status
Not open for further replies.

Patricia Cu

Programmer
Mar 16, 2018
30
CA
Hello all,
For some unknown reason, when using a form with a listbox whose rowsourcetype is Alias (2), if the listbox loses focus, then the user attempts to scroll it, the selection(s) are lost.
The only time selections are retained is when scrolling on a listbox in which selections are currently being made.
I've tried setting a breakpoint on the listbox's LostFocus event and watching the alias' contents in the Data Session window to see when the line ceases to be selected, but that window only ever shows a single line (the most recently clicked) as being selected, no matter how many are selected. The line in the Data Session window remains selected at all times, even when the listbox on the form shows the line as no longer selected.
I have failed to reproduce this on some listboxes which use an Array (5) rowsourcetype, but I can't guarantee that every rowsourcetype except Alias is working.

I'm including a couple of screenshots that demonstrate that no matter how many items I select on the listbox, the alias contents only show one item as selected, which makes my approach to debug the initial issue useless.

Any suggestions on how to figure out why the items become de-selected is appreciated.

Regards,
Patricia Cu
 
 https://files.engineering.com/getfile.aspx?folder=ffd12efe-ade5-4e11-805a-0b496e17fef8&file=Listbox_deselects_items.png
It happens with GotFocus and I donÄt see a way to suppress this. With nodefault there you also suppress being able to ficus and to select items at all.
A multiselect listbox worked for me with a cursor, I developed a moverbox control (two listboxes with some select/deselect buttons in between) using cursors.

No idea what causes this, but the alternative to use the liszbox with additem works, or using rowsourcetype array. Keep the listbox completely unbound at design time, rowsource empty and rowsourcetype 0 (none. Then in init do:
Code:
*Listbox init
AddProperty(this,"aItems[1]",.f.)
Select * from cursor into array  this.aItems

this.Rowsource = "this.aItems"
this.RowsourceType = 5

Of course also set details about boundto/boundcolumn, columncount etc.
The selected items will persist.

Bye, Olaf.

Olaf Doschke Software Engineering
 
...where "cursor" is the alias of your cursor or table. The query may only put some rows into the array, when you used a filter, the array must have only items you want listed.

And besides that, the alias in a datasession always only has one current row. The way to get all selected items from a listbox which stays in focus is by going through the listbox.selected array from listbox.selected(1) to listbox.selected(listbox.listcount) or see whether listbox.selected(listbox.listitemid) is true and set it false to let listbox.listitemid point out the next selected item, an alias, a workarea is never muliselect, the selections are maintained in the listbox.selected array.

And it seems by anything I don't know nowadays the selections are reset every time a listbox is focused when something else had focus. It doesn't happen beforehand, you can still read out selections after lostfocus. which confirms the selections are only lost when focus gets back to the listbox.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Hi Olaf,

Apologies for the late response, we were implementing a proof of concept with your suggestion to use an array to run it by our Project Manager.

Thank you for your input, it is always appreciated!

Regards,
Patricia Cu
 
Good, I'm sure it works with an array and since a listbox only enables selecting items and not editing data you also don't have to store back any array changes.
The only difference in using an array is once the array is created it does not update with data in the table or cursor, so you might need to overwrite the array.

When using a listbox with alias or fields rowsourcetype the selections remain intact even until the GotFocus event, so it can be read out at that moment. But as aftermath the native behavior removes selection. If you suppress that with NoDefault you also forbid the listbox to get focus.

So arrays are the best and simplest solution.

Bye, Olaf.

Olaf Doschke Software Engineering
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top