Solved

Delphi StringGrid component

Posted on 2000-03-15
11
1,081 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
creating manifest for my dll that called from activex 6 98
Dev Express grid collapse 2 39
Controlled Assessment GCSE - desperate help needed 4 75
Base1 Encode/Decode 3 67
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

929 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

11 Experts available now in Live!

Get 1:1 Help Now