Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: TStringGrid Evidenced Cell



Permlink Replies: 5 - Last Post: Jun 5, 2017 2:45 AM Last Post By: Paolo Valle
Paolo Valle

Posts: 39
Registered: 8/31/07
TStringGrid Evidenced Cell
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 1, 2017 11:27 PM
Hello,

I wrote some code to achieve the following effect: when I move the mouse on a tstringgrid I would like to evidence the column and row below the mouse making the whole column and row of different color (it makes more evident to the user what is going to select in a large stringgrid). My code seems not so elegant and in some cases the stringgrid seems buggy, can you suggest how accomplish such task ?
thank you in advance.
Roy Lambert

Posts: 1,063
Registered: 8/7/01
Re: TStringGrid Evidenced Cell
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 2, 2017 12:37 AM   in response to: Paolo Valle in response to: Paolo Valle
Paolo

Use the stringgrids OnMouseMove event then you need something like

function TnlhDBGrid.ColumnUnderMouse: integer;
var
mp: TPoint;
begin
mp := ScreenToClient(Mouse.CursorPos);
Result := MouseCoord(mp.X, mp.Y).X;
if dgIndicator in Options then Result := Result - 1;
end;

function TnlhDBGrid.RowUnderMouse: integer;
var
mp: TPoint;
begin
mp := ScreenToClient(Mouse.CursorPos);
Result := MouseCoord(mp.X, mp.Y).Y;
if dgTitles in Options then Result := Result - 1;
end;

and the grids OnDrawCell event to do the colouring

Roy Lambert

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: TStringGrid Evidenced Cell
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 2, 2017 10:36 AM   in response to: Paolo Valle in response to: Paolo Valle
Paolo Valle wrote:

My code seems not so elegant and in some cases the stringgrid seems
buggy, can you suggest how accomplish such task ?

Use the OnMouseMove event to keep track of which column and row is
under the mouse, and then use the OnDrawCell event to custom-draw cells
that share the same column/row. For example:

var
  CurrentCol: Longint = -1;
  CurrentRow: Longint = -1;
 
procedure TMyForm.StringGrid1MouseMove(Sender: TObject; Shift:
TShiftState; X, Y: Integer);
var
  Col, Row: Longint;
begin
  StringGrid1.MouseToCell(X, Y, Col, Row);
  if (CurrentCol <> Col) or (CurrentRow <> Row) then
  begin
    CurrentCol := Col;
    CurrentRow := Row;
    StringGrid1.Invalidate;
  end;
end;
 
procedure TMyForm.StringGrid1DrawCell(Sender: TObject; ACol, ARow:
Longint; Rect: TRect; State: TGridDrawState);
begin
  if (ACol = ACurrentCol) or (ARow = CurrentRow) then
  begin
    StringGrid1.Canvas.Color := ...;
    StringGrid1.Canvas.FillRect(Rect);
 
    StringGrid1.Canvas.Font.Color := ...;
    Canvas.TextRect(ARect, ARect.Left+2, ARect.Top+2, Cells[ACol,
ARow]);
  end;
end;


--
Remy Lebeau (TeamB)
Paolo Valle

Posts: 39
Registered: 8/31/07
Re: TStringGrid Evidenced Cell
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 2, 2017 12:34 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks both,

Remy the code seems fine and working as expected (there was some typos here I re-post the code with corrected typos for any other users want to use it)

thanks again

var
CurrentCol: Longint = -1;
CurrentRow: Longint = -1;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if (ACol = CurrentCol) or (ARow = CurrentRow) then
begin
StringGrid1.Canvas.Brush.Color := clyellow;
StringGrid1.Canvas.FillRect(Rect);

StringGrid1.Canvas.Font.Color := clblack;
StringGrid1.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, StringGrid1.Cells[ACol,ARow]);
end;
end;

procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
Col, Row: Longint;
begin
StringGrid1.MouseToCell(X, Y, Col, Row);
if (CurrentCol <> Col) or (CurrentRow <> Row) then
begin
CurrentCol := Col;
CurrentRow := Row;
StringGrid1.Invalidate;
end;
end;

Remy Lebeau (TeamB) wrote:
Paolo Valle wrote:

My code seems not so elegant and in some cases the stringgrid seems
buggy, can you suggest how accomplish such task ?

Use the OnMouseMove event to keep track of which column and row is
under the mouse, and then use the OnDrawCell event to custom-draw cells
that share the same column/row. For example:

var
  CurrentCol: Longint = -1;
  CurrentRow: Longint = -1;
 
procedure TMyForm.StringGrid1MouseMove(Sender: TObject; Shift:
TShiftState; X, Y: Integer);
var
  Col, Row: Longint;
begin
  StringGrid1.MouseToCell(X, Y, Col, Row);
  if (CurrentCol <> Col) or (CurrentRow <> Row) then
  begin
    CurrentCol := Col;
    CurrentRow := Row;
    StringGrid1.Invalidate;
  end;
end;
 
procedure TMyForm.StringGrid1DrawCell(Sender: TObject; ACol, ARow:
Longint; Rect: TRect; State: TGridDrawState);
begin
  if (ACol = ACurrentCol) or (ARow = CurrentRow) then
  begin
    StringGrid1.Canvas.Color := ...;
    StringGrid1.Canvas.FillRect(Rect);
 
    StringGrid1.Canvas.Font.Color := ...;
    Canvas.TextRect(ARect, ARect.Left+2, ARect.Top+2, Cells[ACol,
ARow]);
  end;
end;


--
Remy Lebeau (TeamB)
Quentin Correll


Posts: 2,412
Registered: 12/1/99
Re: TStringGrid Evidenced Cell
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 2, 2017 4:25 PM   in response to: Paolo Valle in response to: Paolo Valle
Paolo,

| I re-post the code with corrected typos for any other users want to
use it)

Thanks!

--

Q -- XanaNews 1.19.1.372 - 2017-06-02 16:25:11
Paolo Valle

Posts: 39
Registered: 8/31/07
Re: TStringGrid Evidenced Cell
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 5, 2017 2:45 AM   in response to: Quentin Correll in response to: Quentin Correll
I added this coed to restore the clean grid when the mose leave it, I hope this is fine

//deafult color
clStrgRdCell : TColor = clInfoBk;

procedure TFrmArrayDesigner.StrGrdExcMouseLeave(Sender: TObject);
begin
clStrgRdCell:=clwhite; //to clean the grid
CurrentCol:=-1; //to unlock the current col row color
CurrentRow:=-1;
StrGrdExc.invalidate; //to paint the grid
inherited; //to be sure all the other task are OK
clStrgRdCell:=clInfoBk; //to set back the deafult color for selected row-col
end;

Quentin Correll wrote:
Paolo,

| I re-post the code with corrected typos for any other users want to
use it)

Thanks!

--

Q -- XanaNews 1.19.1.372 - 2017-06-02 16:25:11
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02