Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 05-03-2015, 01:37 AM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default Stringgrid Wrap Not Working

I am trying to get a stringgrid to word wrap using a version of code everyone seems to advocate but it's not working. Can anyone help.

Trial version below:-

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;
    procedure Button1Click(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.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  Rect: TRect; State: TGridDrawState);
var
  S: String;
  drawrect :trect;
begin
  S:= (Sender As TStringgrid).Cells[ Col, Row ];
  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[row]
    Then
      (Sender As TStringgrid).RowHeights[row] :=
        (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.
Form code is:-

object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 202
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 = 120
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect]
TabOrder = 0
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
end
Reply With Quote
  #2  
Old 05-03-2015, 09:42 AM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

It's working fine at my side(XE4):
Reply With Quote
  #3  
Old 05-03-2015, 11:24 PM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default

Hmmmm! I'm using Windows 7 and XE5 Starter. Don't tell me it's yet another instance where you only get part functionality with the starter edition!

Alternatively, perhaps some setting is different, although I can't figure out what it would be. Kolbasz, did you use the same settings for the stringgrid as I had on the form code?

I've been reading some other posts and just realised that Ctrl/Enter doesn't put a return in either.

Badger

Last edited by Badger; 05-04-2015 at 12:00 AM. Reason: Additional info
Reply With Quote
  #4  
Old 05-04-2015, 03:05 AM
GeoWink GeoWink is offline
Senior Member
 
Join Date: Jan 2001
Posts: 885
Default

I did that using Delphi 2009 and it works fine.

George
Reply With Quote
  #5  
Old 05-04-2015, 06:55 AM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

@Badger

Can you please test it, with a now starter edition?
Reply With Quote
  #6  
Old 05-04-2015, 07:56 AM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default

Perhaps I'm getting somewhere.

Firstly, the CTRL+Enter does work - but, as someone pointed out in a thread some time ago, it doesn't retain the multi line format when you move to another cell.

Secondly, I put a break on the first line of the StringGrid1DrawCell procedure and it never got there - qwerty qwerty etc appeared as one line without triggering the drawcell procedure. So why wouldn't a change to the cell trigger it?

Badger
Reply With Quote
  #7  
Old 05-12-2015, 08:50 PM
Hanyx Hanyx is offline
Junior Member
 
Join Date: Feb 2015
Posts: 4
Default

Try to update to the new version.

here works fine.
Reply With Quote
  #8  
Old 05-13-2015, 05:38 AM
Badger Badger is offline
Member
 
Join Date: Aug 2013
Location: Sheffield, Tasmania (Aust)
Posts: 76
Default

Finally figured it out. I deleted the declaration and OnDraw procedure that I had copied whole from a web page and installed a new one by clicking the OnDraw in the object inspector then populated it with the relevant code. Works perfectly.

Well, almost. When you edit the cell the multiline format reverts to single line till you leave the cell. Anyone know a way to prevent this?

Badger
Reply With Quote
Reply

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 09:51 AM.


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