Fifty ways to leave your listbox

Actually, I just want one - the right one - since I’m probably doing something wrong at the moment. I have two listboxes. The first is populated by a method triggered when data has finished coming back from a REST api call. As I click on one row or another, or use the up/down keys, I want to use the current selection to load content into other fields on the form, including another listbox. I’ve been using the Change event to do this, because I know no better, but I’m beginning to wonder if my strategy is right. For one thing, it seems like the event is also triggered when I leave (click out of) the listbox, and I’d rather that didn’t happen.

So two questions really. First, if I want to take some sort of action as a result of clicking a (cell in a) listbox row, or moving up and down with the keyboard, is the listbox.change event the best place to put the code? Or is there a better choice?

Second, is there a way that I can prevent the listbox.change event from firing when I click on another control on the form?

check the listbox.listIndex property inside the changed event. If it’s -1 then they clicked below the content or the selection was otherwise cleared. You can use that to clear the other information displays that are based on the selection.

If it’s a multi-selection listbox you can check the selCount property and if it’s 0 then do the same, otherwise build your display normally.

Change is the correct event. It should not fire when the listbox loses focus.

Tim is right: Change is the right place to handle events. One other tip: You can track the last used index so that it is easy to tell when change fires if the index has actually changed or not. I generally subclass listbox, and add a variable like lastIndex to the subclass. The last thing that change does is set lastIndex = whatever the listbox index is now. The first thing that change does is determine if lastIndex is equal to whatever the index is now. If it is, then just return from the change event, as there is nothing to do. This will prevent the listbox change event from taking any action when it is inadvertently fired but the list selection has not actually changed.

That seems like an unfortunate name - see https://documentation.xojo.com/index.php/ListBox.LastIndex

I have not noticed that this happens, when does it fire without having actually made a change? I definitely do have to check to make sure that listindex is not -1 as just losing focus will cause the event to fire with a -1 which will blow up any accesses you try to make into the listbox to see what was selected :wink: but if it fires extra times I’d like to know about that since I might make my drawing code work faster if it’s doing some drawing without actually needing to.

@Markus Winter - of course you are correct. This is what I get for shooting from the hip when drooling on the forum. Name it something unique, that is not reserved, of course. Perhaps: TheLastIndexIUsedThatActuallyMattered :wink:

I seem to recall needing to deal with this with a few scenarios: Imagine several listboxes on a single window, where selecting an item in the first listbox updates the contents of the other 2 listboxes, and it also draws a picture into a canvas and perhaps loads a url in an embedded HTML Viewer. On Windows I’ve run into problems where clicking into the browser would fire a change event in one of the listboxes. I’ve also seen it where selecting a different application then coming back to the Xojo one would fire a change event. I’ve also seen it happen when opening a different window within the Xojo app and then closing it (returning you back to the first window with the listboxes) would do it.

It’s been quite some time since I’ve seen this problem though… it may have been a bug that was fixed, or I may have just not tried to add a listbox to a new project that would behave in this manner since fixing mine in the way I described above… but it has definitely bitten me in the past, causing LOTS of extra drawing that was completely unneeded.

Yes, it’s what I’m seeing. I put a msgbox into the Change event listing and that msgbox comes up when I change within the list, and also when I click out of the listbox (say, just onto the background of the window).

However, I think I have a good way ahead between the various answers here. Thanks very much.