|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hey there,
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!) |
|
#2
|
||||
|
||||
|
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. Code:
var
nCol : integer;
nRow : integer;
begin
for nCol := 1 to StringGrid1.ColCount - 1 do
begin
nRow := StringGrid1.Cols[nCol].IndexOf('47');
if (nRow > -1) then
ShowMessage('Found it at Column: ' + IntToStr(nCol) + ' and Row: ' + IntToStr(nRow));
end;
end;
__________________
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 Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month |
|
#3
|
|||
|
|||
|
Thanks alot, I spent a long while looking for a solution and the speed of your reply has been extremely helpful. Thanks again!
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Delphi Programming > Searching for a string within a StringGrid (for a board-games board clone) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|