1

Am having a string grid(TStringGrid) with 2 column and 1 row (Property: ColCount = 2 & Rowcount = 1.

Code for OnDrawCell Event:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
  var
    Parametertext : string;
begin
     case ACol of
     0 : Parametertext := 'Test';
     1 : Parametertext := 'Test1';
     end;
     stringgrid1.Brush.Color := clBtnFace;
     stringgrid1.Font.Color := clWindowText;
     stringgrid1.Canvas.FillRect(Rect);
     DrawText(stringgrid1.Canvas.Handle, PChar(parameterText), -1, Rect,
      DT_SINGLELINE);
end;

When I run the application, I get the below output: Sample Output

Question:

When I try to get the text using StringGrid1.Cells[0,0] , StringGrid1.Cells[1,0] ,

I except "Test" & "Test1" but it always gives a empty string"".

How can I get the text from string grid using StringGrid.Cells[aCol,aRow]?

LU RD
  • 34,438
  • 5
  • 88
  • 296
bejarun
  • 175
  • 3
  • 11
  • 1
    I agree with @Dsm's answer, but why are you doing what you're doing in the DrawCell event in the first place? Why not assign the cells' values in code and then leave the grid to do the drawing? – MartynA Feb 22 '17 at 16:27
  • The text isn't there. You've drawn (using **DRAWText**) some text you assigned to a local variable. Why would you then expect that local variable's content to magically be stored in the cells? All you've done is **drawn it**. Here's the magic you're looking for - delete your OnDrawCell handler, and create an OnCreate handler for the form. Add these two lines: `StringGrid1.Cells[0, 0] := 'Test'; StringGrid1.Cells[1, 0] := 'Test1';`. Magic. – Ken White Feb 22 '17 at 18:59
  • This is an existing code not the new one, am not having privilege to modify it. The same type of code is integrated with a large application and am trying to get the Grid value from automation tool (like test complete)... – bejarun Feb 23 '17 at 06:44
  • Is there any other way to get the cell value which is generated by `DrawText` function – bejarun Feb 23 '17 at 06:48
  • If you can't modify the code, then sorry, you are stumped. What you are seeing in the cell is a picture of some text, not the text itself. Short of using some character recognition software (which would be absurd) there is not much you can do, – Dsm Feb 23 '17 at 09:40
  • @Dsm , thanks for your comments. – bejarun Feb 23 '17 at 09:47

2 Answers2

3

You are generating the text to draw it, but not storing it. You also need to set the stringGrid.Cells value, probably not in the OnDrawCell event, though.

Think about your variable Parametertext. It is a local variable destroyed on exit. Nowhere do you save it anywhere else. So why would you expect it to magically appear in the cells property?

Dsm
  • 5,870
  • 20
  • 24
0

To do what you are asking, you need to actually store the string values in the Cells property, not generate them dynamically in the OnDrawCell event:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  Parametertext : string;
begin
  Parametertext := StringGrid1.Cells[ACol, ARow];
  StringGrid1.Brush.Color := clBtnFace;
  StringGrid1.Font.Color := clWindowText;
  StringGrid1.Canvas.FillRect(Rect);
  DrawText(StringGrid1.Canvas.Handle, PChar(ParameterText), Length(ParameterText), Rect, DT_SINGLELINE);
end;

...

StringGrid1.Cells[0, 0] := 'Test';
StringGrid1.Cells[1, 0] := 'Test1';

If you are not going to use the Cells property to store strings, you may as well have just used TDrawGrid instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • thanks @Remy Lebeau.. Is there any other way to get the cell value which is generated by `DrawText` function? Actually am trying to automate the Delphi application using Test Complete Tool. – bejarun Feb 23 '17 at 06:51
  • @bejarun Unless you directly hook `DrawText()` itself, or use OCR technology to read on-screen drawings, then no. – Remy Lebeau Feb 23 '17 at 15:43