#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Devon
    Posts
    8
    Rep Power
    0

    Question Searching for a string within a StringGrid (for a board-games board clone)


    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. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,642
    Rep Power
    4247
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Devon
    Posts
    8
    Rep Power
    0
    Thanks alot, I spent a long while looking for a solution and the speed of your reply has been extremely helpful. Thanks again!

IMN logo majestic logo threadwatch logo seochat tools logo