Solved

String grid column resize

Posted on 1998-09-01
7
1,867 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
  • 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now