Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trying to resize a column in a TStringGrid

750 views
Skip to first unread message

Rafael Castellar das Neves

unread,
Mar 2, 2005, 6:39:44 AM3/2/05
to

Hi!!

I'm trying to resize any column in a TSTringGrid after the user double click the vertical line on the right of that column(the line between this and the next right column). Just like the most applications does...Excel for example. But, my problem lies on how to discover that the user has double-clicked this verticall line? Does any body know how to catch this double click?

Thank you very much,

Rafael

Rafal Z

unread,
Mar 2, 2005, 9:12:22 AM3/2/05
to

Is this very important to you to resize by doubleclick? TStringGrid has
own capabilities to resize columns as well as rows:

StringGrid1.Options:=StringGrid1.Options+[goColSizing,goRowSizing];

Then you see the cursor changing above the gridlines, so you can move
such a gridline.

eshipman

unread,
Mar 2, 2005, 9:52:36 AM3/2/05
to
In article <4225a600$1...@newsgroups.borland.com>,
rafa_ca...@yahoo.com.br says...

Heres something I just whipped up that could help you figure it out,
Can only handle the singleclick;

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs, Grids, Contnrs;

type
TRectObj = class(TObject)
FRect: TRect;
end;

TForm1 = class(TForm)
StringGrid1: TStringGrid;
var CanSelect: Boolean);
procedure StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
rectlist: TObjectList;
procedure CalcRects;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
i: Integer;
rect: TRect;
begin
ShowMessage(IntToStr(X)+','+ IntToStr(Y));
for i := 0 to rectlist.Count-1 do
begin
Rect := TRectObj(rectlist.Items[i]).FRect;
if PtInRect(Rect, Point(x, y)) then
begin
StringGrid1.ColWidths[i] := 100;
// need to readjust the rects;
CalcRects;
end;
end;
end;

procedure TForm1.CalcRects;
var
RectObj: TRectObj;
x, y, i, j, k, l, t: Integer;
begin
rectlist.Clear;
// build a rect for each verticle line:
for x := 0 to StringGrid1.FixedRows-1 do
begin
// skip first col
t := 0;
for y := 0 to StringGrid1.ColCount-1 do
begin
RectObj := TRectObj.Create;
// give a 6 pixel space to click on.
i := t + StringGrid1.ColWidths[y]-3;
j := t + StringGrid1.ColWidths[y]+3;
k := 0;
l := StringGrid1.RowHeights[0]; // height of fixed row
RectObj.FRect.Top := k;
RectObj.FRect.Left := i;
RectObj.FRect.Right := j;
RectObj.FRect.Bottom := l;
t := t + StringGrid1.ColWidths[y];
rectlist.Add(RectObj);
end;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
rectlist := TObjectList.Create(True);
CalcRects;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
rectlist.Free;
end;

end.

Rafael Castellar das Neves

unread,
Mar 2, 2005, 10:02:45 AM3/2/05
to

Hi Rafal!!

I'm already using these options, I just want to improve a way to resize them automatically, by double clicking in the verticall separators lines between the column, like Excel. And my problem is how to catch these double click over these lines. If I can catch them, then I can execute some calcs to determine the new width of the column.

Thank you,

Rafael


Rafael Castellar

unread,
Mar 2, 2005, 10:08:59 AM3/2/05
to

Hi Eshipman!!
I didn't understand in your suggestion where I can catch the double click in the verticall separator lines. I have an idea of how to do the calcs to the new width of such column, but can't catch that double click.

Thank you very much for the suggestion...I'll use this too.

Regards,

Rafael

Peter Below (TeamB)

unread,
Mar 2, 2005, 2:06:27 PM3/2/05
to

The grids OnDblClick event fires when the user clicks on the divider, even if it
is done between header cells. You can get the mouse position from Mouse.Cursorpos,
convert it to grid client coordiantes via the grids ScreenToClient methods, then
try to figure out where the click has landed. Use MouseToCell for a rough estimate,
then check whether the mouse was close enough to the left or right edge of the
cell to qualify as a click on the devider. The CellRect method gives you the
coordinates of the cell. You may need to handle clicks just right of the rightmost
divider specially, though.


--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


Rafael Castellar

unread,
Mar 2, 2005, 2:39:00 PM3/2/05
to

Hi Peter!!

You gave me some good ideas. I'll try them...thank you very much!!

Regards,

Rafael

0 new messages