Solved

String grid column resize

Posted on 1998-09-01
7
1,934 Views
Last Modified: 2010-05-18
How can I detect when a column in a string grid has been resized in Delphi 3.
0
Comment
Question by:nhoj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Expert Comment

by:333
ID: 1338354
Hi,
there is onDrawCell event. But it occurs whenever the contents of a grid cell need to be redisplayed. So it occurs not just when the column or row has been resized, but also when cell or row has been selected, scrolled etc. There are also ColWidthsChanged and RowHeightsChanged methods. They're called when the row or column has been resized. If you use the RxStringList from RxLibrary, there is onColumnSized and onRowSized events.

AP
0
 

Accepted Solution

by:
clopez earned 50 total points
ID: 1338355
It is very simple to detect when a Column is resized.  There is a dinamic procedure on the base component that is executed each time you resize a column.  Overriding it you can launch a notify event.  So you just have to create a new component and register it.  Do it as follow

1.- Create a unit with the following content:

unit NewStringGrid;

interface

uses
  Classes, Grids;

type
  TNewStringGrid = class(TStringGrid)
  private
    fOnColWidthChange:TNotifyEvent;
    procedure ColWidthsChanged; override;
  published
    property OnColWidthChange:TNotifyEvent read fOnColWidthChange write fOnColWidthChange;
  end;

procedure Register;

implementation

procedure TNewStringGrid.ColWidthsChanged;
begin
  inherited;
  if assigned(fOnColWidthChange) then fOnColWidthChange(Self);
end;

procedure Register;
begin
  RegisterComponents('Samples', [TNewStringGrid]);
end;

end.

2.- Add it to the user library or where you wan't and build it.

You will find the TNewStringGrid component under the samples page.  Use it assigning the OnColWidthChanged event.


0
 
LVL 1

Author Comment

by:nhoj
ID: 1338356
Thanks clopez

It works, I can now detect column resizing, but I need to know
which cloumn is being resized. Is this possible ?

nhoj
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:333
ID: 1338357
I think you can use the MouseToCell method. There is simple ex. (use it with OnColWidthChange event):

procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  Column, Row: Longint;
begin
  StringGrid1.MouseToCell(X, Y, Column, Row);
  if Column=0 then begin
   {the row Row was resized}
  end;
  if Row=0 then begin
   {the column Column was resized}
  end;
end;

AP
0
 

Expert Comment

by:clopez
ID: 1338358
I was trying to do so, but the component doesn't give any information about which was the column resized.  

My best idea so long is to keep track of then last change and check on the event;

interface

  TForm1 = class(TForm)
    StringGrid1: TNewStringGrid;
    procedure FormCreate(Sender: TObject);
    procedure OnColWidthChange(Sender: TObject);
  private
     LastSizes:array[0..ncols] of integer;
  public
     { Public declarations }
  end;

implementation

procedure TForm1.FormCreate(Sender: TObject);
var Count:Integer;
begin
   for Count:=0 to StringGrid1.ColCount-1 do
      LastSizes[Count]:=StringGrid1.ColWidths[Count];
end;

procedure TForm1.OnColWidthChange(Sender: TObject);
var Count:Integer;
     Found:Boolean;
begin
   Count:=0;
   Found:=False;
   while (Count<StringGrid1.ColCount) and not found do
      if LastSizes[Count]=StringGrid1.ColWidths[Count] then
         inc(Count)
      else
         Found:=True;
   if Found then
   begin
      LastSizes[Count]:=StringGrid1.ColWidths[Count];
//   The colum resized was "Count"
//  .........
   end;
end;
0
 
LVL 2

Expert Comment

by:333
ID: 1338359
As I said, use MouseToCell method. The Row and Column specifies row and column of grid, on which mouse button was pressed. When you resize row or cell, you press mouse button and MouseToCell returns column and row that was resized.
0
 
LVL 1

Author Comment

by:nhoj
ID: 1338360
Thanks chaps, thats all I need to know.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question