Solved

Delphi StringGrid component

Posted on 2000-03-15
11
1,112 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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