October 20th, 2004, 05:47 PM
Searching for a string within a StringGrid (for a board-games board clone)
I've been programming a string grid to display a variety of numbers in a pattern (this is used for replicating board games such as snakes and ladders where the numbers run to and fro)
I've just finished the board so that when I click the desired button, the stringgrid arranges itself how I want it to. However I do not now know how to search for the value of the String. For example somewhere in the grid is a number "47" however I do not know how to search for this number and replace the cell with "47" in with something like "Player 1 position."
It may be simple, it may be impossible. But any ideas?
(PS please tell me if im not making much sense!)
October 20th, 2004, 06:03 PM
Pretty easy actually. Note that the StringGrid has a property called Cols which returns a specific column as a TStrings. It also has a property called Rows, that returns a specific row as a TStrings object. Either way, a TStrings has an IndexOf() method to find a string within it. So, here's our method:
1. Go through all the columns of the stringgrid using the Cols property.
2. For each one, use IndexOf() to see if the string exists in this column. If it does, this is the row position of the item.
nCol : integer;
nRow : integer;
for nCol := 1 to StringGrid1.ColCount - 1 do
nRow := StringGrid1.Cols[nCol].IndexOf('47');
if (nRow > -1) then
ShowMessage('Found it at Column: ' + IntToStr(nCol) + ' and Row: ' + IntToStr(nRow));
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
October 20th, 2004, 06:30 PM
Thanks alot, I spent a long while looking for a solution and the speed of your reply has been extremely helpful. Thanks again!