Selecting a listbox row based on a value?

To refresh the canvas image on a listbox row I have to first select another row and then go back to the original row. I am trying to get this done automatically, so at the end of the code in the DropObject event of the Canvas I added this code:

ListBoxCharacters.Selected(ListBoxCharacters.SelectedRowIndex + 1) = true
ListBoxCharacters.Selected(ListBoxCharacters.SelectedRowIndex - 1) = true

However when the selected row is the last row, it cannot go to a next row so this doesn’t work.

I tried refreshing the canvas with winmain.canchar1.Refresh command but that doesn’t work either.

So now I am trying to get back to the previously selected row which would refresh the canvas image, by placing the Name column value of the listbox in a property, then selecting another row and then using the property to get back to the original selected listbox row.

To get the value I use this code:
selectedCharRow = ListBoxCharacters.CellValueAt(ListBoxCharacters.SelectedRowIndex,0)

Then I move to the last row in the listbox with:
ListBoxCharacters.Selectedrowindex = ListBoxCharacters.LastAddedRowIndex

How do I now get the row selected that carries the property value? I tried this, but that doesn’t work:
ListBoxCharacters.Selectedrowindex = ListBoxCharacters.SelectedRowIndex = selectedCharRow

seems you are using a selected change event from the list to redraw your canvas.

if you call Refresh your UI Element will Redraw and you get a Paint Event
in your Paint Event you must know what you will draw so you need a property (a listbox index?).
seems every row contains a “image” which you will display.

what are you doing in this DropObject event in the canvas?
i guess you add an entry to the listbox, true?

subclass your canvas so you can add your own propertys and methods there.

Hi Markus, thanks for the advice but I don’t understand what you are saying, also, what does it mean to subclass the canvas? What I want is to refresh the selected listbox row. This happens when I click twice on the row or by going to another row and back again.

DesktopListBox RefreshCell

You should move whatever is in SelectionChanged that tells the canvas exactly what to draw somewhere else.

As a really terrible workaround until you do so, you can use SelectedRowIndex = -1 to select no row at all, then restore the selected row.

Written in the post editor on an iPad at 5am disclaimer…

var iSel as Integer = MyListbox.SelectedRowIndex
MyListbox.SelectedRowIndex = -1
MyListbox.SelectedRowIndex = iSel

Thanks Tim, maybe it’s not an elegant solution as you say, but it works perfectly! :slight_smile: I have been struggling on this for the last 2 days.