Listbox CellClick event not triggered

Hi - I’m finding that when I open a form with a ListBox and click a cell/row, my CellClick event doesn’t trigger until a second single-click (which doesn’t necessarily need to be on the same row). The ListBox doesn’t seem to want to trigger an event until after it has been clicked and made active. After the first click with no response, all single-clicks trigger the CellClick event as expected – even if I’ve clicked on another control on the same window and/or typed something in a textbox, etc. How can I ensure that the CellClick event is also triggered the first time I click on the window/ListBox control?

what platform ?

I am not seeing that behavior here on Mac.
Here’s an example project to test/play with: https://dl.dropboxusercontent.com/u/10504478/lbclick.xojo_binary_project

I would look very closely at the code. I have never seen this behavior on Mac or Windows. And this very afternoon, I was working on a Listbox where cellclick worked flawlessly on Windows with 2016R4.1. Would you be doing stuff in mousedown by any chance ?

This is on Windows. Since I’m still experimenting, I don’t have a lot of code nor am I using many events. There’s nothing in MouseDown, The fact that I have put so little into this window and the ListBox so far, I can’t imagine what might be affecting the triggering/lack of triggering of the event.

For the ListBox, I have the following basic code to help with debugging:

Open:

	Me.ColumnCount = 5
	
	Me.HasHeading = True
	Me.Heading(0) = "Item"
	Me.Heading(1) = "Setting"
	Me.Heading(2) = "Description"
	Me.Heading(3) = "Possible Values"
	Me.Heading(4) = "Notes"
	
	Me.ColumnWidths = "300,200,300,100,300"

CellClick:

	if Me.ListIndex <> -1 then
			msgbox "CellClick event launched..." + str(Me.cell(row,0))
	end if

DoubleClick:

	btnModListItem.Push
  • Scott

Do you see the behavior in the test project I posted?

Thank you… I will download and review that next…

[quote=309720:@R. Scott Poppele]CellClick:

if Me.ListIndex <> -1 then
msgbox “CellClick event launched…” + str(Me.cell(row,0))
end if
[/quote]
The first time you click the listbox, it won’t have a row selected, so ListIndex will be -1. I think the event may be firing, but you’re just not seeing the msgbox.

The code you have here doesn’t need ListIndex anyway. Row will always be in valid (the event doesn’t fire on an empty row). Note that ListIndex is not updated to the new row until after the CellClick event.

[quote=309720:@R. Scott Poppele]This is on Windows. Since I’m still experimenting, I don’t have a lot of code nor am I using many events. There’s nothing in MouseDown, The fact that I have put so little into this window and the ListBox so far, I can’t imagine what might be affecting the triggering/lack of triggering of the event.

For the ListBox, I have the following basic code to help with debugging:

Open:

	Me.ColumnCount = 5
	
	Me.HasHeading = True
	Me.Heading(0) = "Item"
	Me.Heading(1) = "Setting"
	Me.Heading(2) = "Description"
	Me.Heading(3) = "Possible Values"
	Me.Heading(4) = "Notes"
	
	Me.ColumnWidths = "300,200,300,100,300"

CellClick:

	if Me.ListIndex <> -1 then
			msgbox "CellClick event launched..." + str(Me.cell(row,0))
	end if

DoubleClick:

	btnModListItem.Push
  • Scott[/quote]

1 - When you post code, please select it and click the code icon above the editor. It will be way more legible.
2 - I would remove any test in CellClick, since all you want to do is see if the event takes place :

CellClick : msgbox "CellClick event launched..." + str(Me.cell(row,0))
At any rate, cellclick cannot happen if no row is there.

Thanks, everyone.

That makes sense - that the CellClick event would not fire without rows in the listbox. However, in code, I actually pre-populate the listbox with 10 lines, so there are rows present when I first click the listbox (and no CellClick event apparently fires) and when I click a second, third, fourth, etc. time (when it does fire). I did move the code in CellClick to the Change property of the listbox, and that does effectively give me the behavior I’m looking for in my current app. Every time I click on a row in the listbox, including the first time, the Change event fires and I have a valid (not -1) ListIndex property to use in next steps.

Tim P - Thanks for the ListBox test project. I inserted my MsgBox notification in CellClick in that project, and it works as expected. The only key difference I can think of here is that I have more than one window in my project (the one with the ListBox is displayed first). My issue with CellClick not firing the first time I see the window with the ListBox happens when the app first starts and window appears, and when I leave and return to that window from within the application.

Tim H - Right, I understand that before I click the listbox there would be no row selected and no ListIndex value. However, to clarify, the behavior I’m describing is that the first time I click the listbox, the text in a row (just the first cell at this point) is highlighted but the CellClick MsgBox is not displayed. Every time I click on any row after that, the correct text for column 0 is displayed in the MsgBox.

In order to demonstrate this behavior, is there a way for me to post/share my app? I’d understand if not because allowing anyone to post code could potentially open the door to people posting malicious code. I’m not “verified” yet because I’ve been just testing Xojo out and making sure it’s right for me after years with VB6, no desire to move to VB.Net, and exploring other languages.

Regardless, I’ll continue experimenting, communicating in the forum (and I think I’ve decided Xojo will be my new dev environment).

Thanks

The event is firing, but your code is making an invalid assumption. CellClick fires before the row is selected/highlighted, in order to give you a chance to disallow the click. If you need to act on the row that was clicked on, use the Row parameter that is passed in. That will be the row that was clicked on. Or move your code to the Change event, which fires after the row is selected. But it also fires for other actions as well, which may or may not be desirable.

CellClick: ListIndex is not valid
Change: ListIndex is valid

If you want code to respond only to a mouse click, but not to any other change in the selection, use CellClick and Row. If you want code that will fire whenever a row is highlighted, use Change and ListIndex.

If you are on Windows, you can place it on your OneDRive and share that file. Then post the URL.

You can also still use dropbox and share a file in the public folder.

BTW we are quite a number of Xojo users coming from Visual Basic :slight_smile: