Select cell

Hello, I would like to select a certain cell among several of a listbox, depending on whether its content matches that of a textfield. I’m trying different options but I can’t get it.
Can anyone share an example? Thanks a lot

Do you mean you want to make it visually distinct from the surrounding cells? or put the cursor in it for editing?

Yes Tim, I want to make it visually distinct from the surrounding cells. Thanks

Then you need to be able to identify the selected cell in either CellBackgroundPaint or CellTextPaint and modify the background color, the border, or the text color to suit your needs.

Yes, this is exactly what I want. The problem is how to do it.

“dim n as integer

For n = 0 to ListBox1.LastRowIndex
If TextField1.Text = ListBox1.CellTextAt(n,0) then
ListBox1.CellBorderColorAt(n,0) = Color.Red
End If

Doesn’t work
(even if it works I would prefer paint the background).


Highlight your code and press the </> button it will make it more readable.

Right. You have to do this in the listbox events. For example in CellBackgroundPaint, you could do

if row < me.RowCount then
   if me.CellTextAt(row, column) = TextField1.Text then
      g.DrawingColor = Color.Red
      g.DrawRectangle(0, 0, g.width-1, g.height-1)

I’m not sure if the “-1” on width and height is required.

Instead of your current code, call Listbox1.Invalidate.



Thank Tim, it works fine. (Perhaps, instead of “invalidate” it is “refresh” now)