Solved

Excel style memo indicator in dbGrid

Posted on 2003-11-12
10
561 Views
Last Modified: 2010-04-05
I would like to make a dbGrid cell look like an excel cell that has a memo attached. That is it has a small red triangle at the top right of the cell, holding the mouse over the red triangel causes a floating dialogue box to appear with the memo.

Help would be appreciated
0
Comment
Question by:alanjbrown
  • 6
  • 4
10 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9737175
Try something like this:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  Grid: TDBGrid;
begin
  Grid := Sender as TDBGrid; // so we don't have to cast the darn thing
  case DataCol of
  0: // Your memo field column number...
    begin
      Grid.Canvas.Brush.Color := clRed;
      Grid.Canvas.Rectangle(Rect.Right-5, rect.Top, Rect.Right, Rect.Top+5);
    end; {case:0}
  end; {case}
  Grid.DefaultDrawing := True;
end;

0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9738961
Thanks EddieShipman.

That is a useful start. Is it possible to display a triangle instead of a rectangle?

I also need to be able to detect when the mouse is over the area so that I can display a panel with the text in it next to the cell.

 
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9739029
I have managed the triange part by using grid.canvas,polygon instead of rectangle.

0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 26

Expert Comment

by:EddieShipman
ID: 9739721
Oh, I thought you said rectangle., sorry...

You have in the TDBGrid.MouseCoords function that you can use in the mouse move
Heres an example from the Borland newsgroups by Frank Plevniak
(found using search at http://www.tamaracka.com)

type
  TMyGrid = class(TDBGrid)
  public
    {Redeclare the DataLink property as public so we can use it}
    property DataLink;
  end;

{...implementation of TForm1...}

procedure TForm1.MouseMove(Sender: TObject; Shift: TShiftState; X, Y:
Integer);
var
  OldActive: Integer
  MyGrid :TMyGrid;
begin
  if not (Sender is TDBGrid)
  then raise Exception.Create('Need TDBGrid to do this!');
  MyGrid := TMyGrid(Sender);
  Coord := MyGrid.MouseCoord(X, Y);
  if (Coord.Y < 0)
  then Exit; {The column header triggers a mouse move event too, must ignore}
  {Now we can look for our hint text}
  OldActive := MyGrid.DataLink.ActiveRecord;
  try
    {This method locates the field the mouse is hovering over using the "relative" grid coordinates}
    MyGrid.DataLink.ActiveRecord := Coord.Y - 1; {Dec() here because the first row is 1 but we need 0 based access}
    {The field value is now available for use, access it to assign the hint!}
    MyGrid.Hint := MyGrid.Fields[0{Hint Text Field}].AsString;
  finally
    dbGrid.DataLink.ActiveRecord := OldActive;
  end;
  {More code goes here to force the display of the new Hint text}
end;
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9739730
However, this would get the coords of the enitre CELL. Let me think about this one for a moment.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9749960
Can you show me how you are creating the polygon and then drawing the triangle?
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9758151
For rectangle :-
 Grid.canvas.Rectangle(rect.right -5, rect.top, rect.right, rect.top +5);

For Triangle :-
Grid.canvas.Polygon([point(rect.right -7, rect.top), point(rect.right, rect.top),point(rect.right, rect.top +7)]);
   
0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
ID: 9763638
What you want to do is something like this:

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  LGrid: TDBGrid;
  LPoints: array[0..2] of TPoint;
  i: Integer;
begin
  LGrid := Sender as TDBGrid; // so we don't have to cast the darn thing
  case DataCol of
  0: // Your memo field column number...
    begin
      LGrid.Canvas.Brush.Color := clRed;
      // Make the region two pixels bigger than the triangle
      LPoints[0] := Point(Rect.Right - 9, Rect.Top);
      LPoints[1] := Point(Rect.Right, Rect.Top);
      LPoints[2] := Point(Rect.Right, Rect.Top + 9);
      Rgn := CreatePolygonRgn(LPoints,3,WINDING);
      LGrid.Canvas.Polygon(LPoints);
    end; {case:0}
  end; {case}
  LGrid.DefaultDrawing := True;
end;

procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if PtInRegion(Rgn, x, y) then
  begin
    ShowMessage('there');
  end;
end;


Of course this will only operate on the lst drawn cell with the triangle so you will need to
devise a way to create a rgn for each cell and store it in some list and then check the
entire list in the mosemove to see if the pt is in any of the regions in the list.
0
 
LVL 1

Author Comment

by:alanjbrown
ID: 9766573
EddieShipman

I am still having difficulty with the mouse move event. I can pick up the mouse move when it is outside the dbgrid but not when it is on the grid.

I have increased the points for this question because, initially I did not realise just how difficult it would be.




0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 9766923
I was able to catch the mousemove on the LAST row. Because it si the last one drawn, it has
the RGN. I suggested to create an array or list of HRGNs, one for each visible row, and then
check each of them in the mousemove to see if the pt is in any HRGN for any of them. Then
do whatever you want when you are on the one.

I will work a little more to see if I can come up with something.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

832 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