Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Delphi StringGrid component

Posted on 2000-03-15
11
Medium Priority
?
1,164 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 800 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

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.

Question has a verified solution.

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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

782 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