Timer to read a selected row in the ListBox

Hi, guys,

Let me describe the challenge, and if anyone can guide me in the right direction, I would highly appreciate it.

In the main window, I have a Listbox and a PagePanel. The ListBox is dynamically populated from the Window2.

Selecting an item in the ListBox tells the PagePanel to go to a specific page. This works well when the users click the item in the ListBox or use keyboard to navigate the ListBox.

The problem arises when the previously selected item in the ListBox is dynamically removed via code from the other window. In this case, the PagePanel shows the same panel as before even though now there is a different item in the Listbox selected. It is easy to explain - the PagePanel has not changed because there was no CellClick event fired.

I am thinking about setting up a timer that would be reading items in the ListBox and:

  1. if there are no items in the ListBox - set the PagePanel.Value=0
  2. if there are items in the ListBox and non is selected - set the PagePanel.Value=0
  3. If there is one ore more item in the ListBox and there is something selected - it will set the PagePanel.Value to the corresponding number based on what exactly is selected in the ListBox.

Is this a right way to address the challenge or should I look for a different mechanism? Any help is highly appreciated.

Thank you,

Val

I presume your code is in the Listbox.Change event?

When you delete a row I would have expected that the event fires anyway, but if not then just raise the event in code.

Hi, Markus,

Thank you for your reply.

In the CellClick I have this code

myrow = row
mycolumn = column

It determines what cell is clicked.

Then in the ListBox.Change

I have the following code

Dim s As String

if me.ListCount > 0 then
if me.Selected(myrow) = true Then
s=me.Cell(myrow, mycolumn)
end if
end if

if s=“Item1” then
PagePanel1.Value=1
elseif s=“Item2” then
PagePanel1.Value=2
elseif s=“Item3” then
PagePanel1.Value=3
elseif s=“Item” then
PagePanel1.Value=4
end

In my challenge, I need to find a replacement of CellClick via Timer to find out which cell is currently selected.

Thank you for your reply.

Val

The PagePanel should change when the ListBox row changes, yes? If so, don’t use CellClick, use the Change event, as Markus alluded.

Kem,

Thank you for the reply. I have the following code in the Change event


Dim s As String

if me.ListCount > 0 then
if me.Selected(myrow) = true Then
s=me.Cell(myrow, mycolumn)
end if
end if

if s="Item1" then
PagePanel1.Value=1
elseif s="Item2" then
PagePanel1.Value=2
elseif s="Item3" then
PagePanel1.Value=3
elseif s="Item" then
PagePanel1.Value=4
end

The question is how to make the Change event know which cell is currently selected? Right now it knows it because the variables myrow and mycolumn are set in the CellClick event.

The question remains the same, how to make Timer to look at the ListBox, identify what column and what cell are selected and put it into a variable.

Thank you,

Val

Listbox.ListIndex

Eli,

Thank you for the reply.

ListBox.Index will not work. Imagine the ListBox being dynamically populated by the names of people who are getting on and off the bus. Clicking an item in the ListBox opens a person’s profile.

Since the ListBox content is not fixed but rather dynamic and is constantly changing over time, ListBox1.ListIndex(1) has a different name attached to it than ListBox1.ListIndex(1) five seconds later.

I need the Timer to find which item in the list is selected and what exactly the name of the that item.

Thank you,

There is no ListBox1.ListIndex(1). ListIndex does not take an argument.

ListIndex will always give you the selected row. This is also true in the Change event. No need for implementing CellClick.

So in the Change event you do something like that:

If Me.ListIndex = -1 Then // No row selected PagePanel1.Value = 0 Else // Get the value from first column of selected row Dim selectedValue As String = Me.Cell(Me.ListIndex, 0) Select Case selectedValue Case "ABC" PagePanel1.Value = 1 Case "DEF" PagePanel1.Value = 2 Case ... ... Else PagePanel1.Value = 0 End End

Thank you, Eli.

Going home to put the code into the project.