Regex Question (Listbox Icon)

Hello all!

I am fairly new to regex (and programming in general) and have a feeling I am using it very wrongly.

In a listbox’s “CellBackgroundPaint” event I have the following code,

var rx as new RegEx
rx.SearchPattern = "Test"

for row = Datalist.LastRowIndex downto 0
  if rx.Search( Datalist.Cell( row, 2 ) ) is nil then
    g.drawpicture Test_20px,0,0
  end if
next

What I was trying to achieve was when the program runs, regex searches the list box (Column 2) for the word “Test” and when it finds it, it tags it with an icon which is stored in the application.

What happens is… nothing, no icon is drawn. But if I change the column to either 0 or 1 (or any column that doesn’t have “Test” in it) it draws the icon into all the cells.

I have a feeling I am going about this very wrong and hope someone can point me in the right direction.

Best regards,

Robin

Why not simply do it like this?

' Xojo 2021r3 Code!
If row > Me.LastRowIndex Then Return False
If column <> 2 Then Return False
If Me.CellTextAt(row, 2).IndexOf("Test") = -1 Then Return False

g.DrawPicture(Test_20px, 0, 0)
Return True
1 Like

If in actuality you are trying to use RegEx, I would recommend moving the search to its own function. You can tag either the cell or row to know whether or not to draw the icon. RegEx can become an expensive process and you wouldn’t want to re-perform a complex search on every draw. Remember, drawing events are for drawing only.

1 Like

Giving my experience in programming, I find myself going around the world for a shortcut on most occasions lol

But that code works perfectly Martin, thank you ever so much!

@Tim_Parnell Thank you for the advice! I am still reading up a lot on Regex at the moment.

Here’s an example I built that illustrates how to separate the search from the drawing code. Even if you’re using Martin’s IndexOf suggestion you should still separate the logic from drawing.

ListboxRx.xojo_binary_project

1 Like

In other words, the question can be:

How many times a second, every second, your RegEx code is executed ? (because you put it in CellBackgroundPaint Event).

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.