Solved

Excel style memo indicator in dbGrid

Posted on 2003-11-12
10
568 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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