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
Solved

Delphi StringGrid component

Posted on 2000-03-15
11
1,108 Views
Last Modified: 2011-10-03
Hi
The standard stringgrid lets the user 'select' a block of cells ie the 'selection property.
I need to modify this property so that the user can only select horizontal or vertical cells, depending in which direction he moved the mouse. I am not too familiar with creating components even with the delphi books I cannot manage to get what I want. I have been advised that I need to modify the stringgrid source ie Grids.pas but I really need a grid based on this
Can you help with the detailed code for a new grid and for calling the new function
thanks
Paul
0
Comment
Question by:pady
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 1

Expert Comment

by:yk030299
ID: 2621953
listen
0
 

Expert Comment

by:westy100697
ID: 2621957
Hi,
   You could probably get around this using a combination of the OnSelectCell and OnMouseMove events. I will try something now and see if I can't help.

WESTY :)
0
 

Expert Comment

by:westy100697
ID: 2621990
Hi again,
         I would definitely use the OnMouseMove event. As you move it you can check whether the X or Y is changing and then set the selection as you go

So like this

 Stringgrid1.Selection.TopLeft := (Your positioning goes here);
 Stringgrid1.Selection.BottomRight := ();

These are in X,Y coordinates and I think it is quite simple from here.


WESTY :)

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Expert Comment

by:westy100697
ID: 2621999
Also you know what the DefaultRowHeight and DefaultColWidths are so it will be easy to calculate where you are.

WESTY :)
0
 
LVL 1

Expert Comment

by:yk030299
ID: 2622032
I hope it helps! :)

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  myRect: TGridRect;
  c:boolean;
  str:string;
begin
 if gdSelected in State then begin
  with StringGrid1.Selection do begin
    str:=    '<===>'+(format('%d-%d(%d,%d;%d,%d)',[ ACol, ARow,Left, Top, Right, Bottom]));
  end;

  myRect:=StringGrid1.Selection;
  with myRect do begin
    c:=(Left<>Right) and (Top<>Bottom);
    if ((Right-Left)<>0) then begin Bottom:=Top; end;
    if ((Bottom-Top)<>0) then begin Right:=Left; end;
  end;
  if c then StringGrid1.Selection:=myRect;

  with StringGrid1.Selection do begin
    str:=str+'<===>'+(format('%d-%d(%d,%d;%d,%d)',[ ACol, ARow,Left, Top, Right, Bottom]));
  end;
  Memo1.Lines.Add(str);
  StatusBar1.SimpleText:=str;
 end;
end;
0
 
LVL 1

Accepted Solution

by:
fulvio_brasil earned 200 total points
ID: 2622345
pady, try this:

First put one TStringGrid on form (StringGrid1)

in form class definition:
....
  private
   {}
  public
    firstC, firstR: integer;
    chgC, chgR: boolean;
    rect: TGridRect;
    second: boolean;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.StringGrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  StringGrid1.MouseToCell(X, Y, firstC, firstR);
  rect.Top := firstR;
  rect.Bottom := firstR;
  rect.Left := firstC;
  rect.Right := firstC;
  second := true;
end;

procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
var ACol, ARow: integer;
begin
  if Shift = [ssLeft] then begin
    StringGrid1.MouseToCell(X, Y, ACol, ARow);
    if ACol = -1 then ACol := StringGrid1.ColCount-1;
    if ARow = -1 then ARow := StringGrid1.RowCount-1;
    if (second) and ((ACol <> firstC) or (ARow <> firstR)) then begin
      chgC := (ACol <> firstC);
      chgR := (ARow <> firstR) and not(chgC);
      second := false;
    end;
    if (chgR) then rect.Bottom := ARow;
    if (chgC) then rect.Right := ACol;
    StringGrid1.Selection := rect;
  end;
end;

procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  StringGrid1.Selection := rect;
end;

It isn't "perfect" but works...

cheers,

Fulvio.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2622366
Following
0
 
LVL 4

Expert Comment

by:Radler
ID: 2623812
Hi pady,

See this way:
//DFM
object Form1: TForm1
  Left = 302
  Top = 105
  Width = 870
  Height = 640
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnMouseUp = GridMouseUp
  PixelsPerInch = 96
  TextHeight = 13
  object Grid: TStringGrid
    Left = 216
    Top = 80
    Width = 433
    Height = 400
    TabOrder = 0
    OnMouseDown = GridMouseDown
    OnMouseUp = GridMouseUp
    ColWidths = (
      64
      64
      81
      87
      128)
  end
  object Vert: TCheckBox
    Left = 224
    Top = 32
    Width = 97
    Height = 17
    Caption = 'Vertical'
    TabOrder = 1
  end
end


//Code **Create a Form <ALT>+<F12> and paste this <ALT>+<F12> again
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Grid: TStringGrid;
    Vert: TCheckBox;
       procedure GridMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
       procedure GridMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
       { Private declarations }
  public
       { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.GridMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
      ACol, ARow : integer;
      Rect : TRect;
begin
      Self.Grid.MouseToCell( X, Y, ACol, ARow );
      Rect:=Self.Grid.CellRect( ACol, ARow );
      if Self.Vert.Checked then begin
            Rect.Top:=Screen.DesktopTop;
            Rect.Bottom:=Screen.DesktopHeight;
      end else begin
            Rect.Left:=0;
            Rect.Right:=Screen.DesktopWidth;
      end;
      Rect.TopLeft:=Grid.ClientToScreen( Rect.TopLeft );
      Rect.BottomRight:=Grid.ClientToScreen( Rect.BottomRight );
      ClipCursor( @Rect );
end;

procedure TForm1.GridMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
      ClipCursor( nil );
end;

end.

Good luck.

T++, Radler.
0
 

Author Comment

by:pady
ID: 2637089
works perfectly
thanks
0
 
LVL 4

Expert Comment

by:Radler
ID: 2637582
Hi pady,

I'd happy with a little comment about my way.

T++, Radler.
0
 

Author Comment

by:pady
ID: 2641190
I studied all the suggestions made to solve my problem and the easiest for me to understand was Fulvio's. I tried it and it did exactly what I wanted.
I was'nt sure how to use the code that Radler kindly wrote for me so I havent mastered it yet
thanks for the hard work
pady
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…

829 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