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.
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.
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.