00 Votes

Lazarus: Multiline StringGrid Cells

Question by Guest | 2016-02-13 at 09:44

The content of some cells of my StringGrid are so long, that the width of the corresponding column is not sufficient. This leads to the behavior, that the content is just truncated.

However, instead of that, I would rather like to have a line break so that the content of the cell is just displayed in multiple lines in that case.

But how can I achieve this? I have not found any property such as WordWrap, WordBreak and MultiLine in the TStringGrid component.

ReplyPositiveNegative
2Best Answer2 Votes

Your StringGrid has the event OnPrepareCanvas. This event is called for each cell and you can use it to control the TextStyle of the cell.

In your case, this can look like that:

procedure TForm1.StringGrid1PrepareCanvas
  (Sender: TObject; aCol, aRow: Integer; 
  aState: TGridDrawState);
var
  ATextStyle: TTextStyle;
begin
  ATextStyle := StringGrid1.Canvas.TextStyle;
  ATextStyle.SingleLine := false;
  ATextStyle.Wordbreak := true;    
  StringGrid1.Canvas.TextStyle := ATextStyle;
end;

Here, we are setting SingleLine to FALSE and WordBreak to TRUE which is making the text displayed with line break in the corresponding cell.

As you can see, there are also parameters for the column and row provided. We can use them to apply different properties for different cells. For example:

procedure TForm1.StringGrid1PrepareCanvas
  (Sender: TObject; aCol, aRow: Integer; 
  aState: TGridDrawState);
var
  ATextStyle: TTextStyle;
begin
  if aCol = 2 then begin
    ATextStyle := StringGrid1.Canvas.TextStyle;
    ATextStyle.SingleLine := false;
    ATextStyle.Wordbreak := true;    
    StringGrid1.Canvas.TextStyle := ATextStyle;
  end;
end;

In this example, we check whether it is the second column and we are changing the behavior only in that case. With this, there is only a line break within the second column while the other comments are showing the default behavior.
2016-02-13 at 11:22

ReplyPositive Negative
Reply

Related Topics

PHP: Sending an E-Mail

Tutorial | 0 Comments

HTML: Table - Merge Cells

Question | 1 Answer

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.