Solved

Delphi StringGrid component

Posted on 2000-03-15
11
1,078 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

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