Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 07-22-2015, 12:10 PM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default Problem with multiple lines in a string grid cell

I am writing strings to a string grid at run time and I need to have the strings wrap where necessary. I think I got the attached code from this forum but can't find it again.

My problem is that the code works perfectly when the string is long and needs wrapping (button1) but seems to overwrite the string with a slight vert & horizontal offset when the string is short (button2).

If it doesn't do it on another machine then the problem is mine. If it does do it, can anyone offer a fix.

Code:
 
unit Unit6;
interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.StdCtrls;
type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    //procedure StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
 // Rect: TRect; State: TGridDrawState);
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
StringGrid1.Cells[2,2]:='qwerty qwerty qwerty qwerty qwerty qwerty qwerty qwerty qwerty qwerty ';
end;
 
procedure TForm1.Button2Click(Sender: TObject);
begin
  StringGrid1.Cells[2,3]:='test';
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  S: String;
  drawrect :trect;
begin
  S:= (Sender As TStringgrid).Cells[ ACol, ARow ];
  If Length(S) > 0 Then Begin
    drawrect := rect;
    DrawText((Sender As TStringgrid).canvas.handle,
              Pchar(S), Length(S), drawrect,
              dt_calcrect or dt_wordbreak or dt_left or dt_noprefix );
    If (drawrect.bottom - drawrect.top) >
      (Sender As TStringgrid).RowHeights[Arow]
    Then
      (Sender As TStringgrid).RowHeights[Arow] :=
        (drawrect.bottom - drawrect.top)
    Else Begin
      drawrect.Right := rect.right;
      (Sender As TStringgrid).canvas.fillrect( drawrect );
      DrawText((Sender As TStringgrid).canvas.handle,
                Pchar(S), Length(S), drawrect,
                dt_wordbreak or dt_left or dt_noprefix);
    End;
  End;
end;
end.
Badger
  #2  
Old 07-22-2015, 04:45 PM
GeoWink GeoWink is offline
Senior Member
 
Join Date: Jan 2001
Posts: 885
Default

Using D2009, and Windows 7. Works fine.

George
  #3  
Old 07-23-2015, 01:50 AM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default

I'm using Windows 7 and XE5. The code for the form is below

object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 321
ClientWidth = 447
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object StringGrid1: TStringGrid
Left = 72
Top = 64
Width = 320
Height = 233
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing]
TabOrder = 0
OnDrawCell = StringGrid1DrawCell
ColWidths = (
64
64
100
97
64)
end
object Button1: TButton
Left = 184
Top = 24
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object Button2: TButton
Left = 288
Top = 24
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 2
OnClick = Button2Click
end
end

The only problem could be the StringGrid Options but I can't see how.


Badger
  #4  
Old 07-24-2015, 02:34 PM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 198
Default

if you set the SG's DrawingStyle property to gdsClassic, then the code works, seems to be an issue when the SG is theme'd
  #5  
Old 07-29-2015, 06:43 AM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default

I'm not ignoring the posts - haven't had a chance to try the latest advice yet.
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 06:43 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.