Hyperlink in a DBGrid field

4

I'm doing a little internal software to search extensions in my company. In addition to the extensions I also put an email field in the database as can be seen below:

My intention is to click on the registered e-mail, the software through ShellExecute open a window to send the e-mail. I'm using the dgRowSelect option as TRUE and because of this the OnCellClick event does not correctly identify which cell was clicked.

In my searches I have not found any way to do yet. So I thought I'd use TLabel inside the field. I can put ShellExecute in event OnClick of TLabel and also change the cursor icon of it.

If TLabel is a good solution, how can I insert TLabel into the cells of a column in the DBGrid?

Or what would be another good solution?

    
asked by anonymous 13.03.2014 / 20:44

1 answer

2

See my unit:

unit untMainForm;

interface

uses
  WinApi.Windows,
  System.Classes, System.SysUtils,
  Vcl.Forms, Vcl.Controls, Vcl.Grids, Vcl.DBGrids, Vcl.Graphics, Vcl.Dialogs,
  Data.DB, Datasnap.DBClient, Vcl.StdCtrls;

type
  TMainForm = class(TForm)
    dbgLink: TDBGrid;
    cdsLink: TClientDataSet;
    dasLink: TDataSource;
    cdsLinkID: TIntegerField;
    cdsLinkLink: TStringField;
    lblCoord: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure dbgLinkMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure dbgLinkCellClick(Column: TColumn);
  private
    mouseCell: TGridCoord;
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.FormCreate(Sender: TObject);
var
  nI: Integer;
begin
  cdsLink.CreateDataSet;

  for nI := 1 to 5 do
  begin
    cdsLink.Append;
    cdsLinkID.AsInteger := nI;
    cdsLinkLink.AsString := '[email protected] ' + IntToStr(nI);
  end;

  cdsLink.Post;
end;

procedure TMainForm.dbgLinkMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  mouseCell := dbgLink.MouseCoord(X,Y);
  if mouseCell.X = 2 then
    Screen.Cursor := crHandPoint
  else
    Screen.Cursor := crDefault;

  lblCoord.Caption := Format('Coordenadas X: %d, Y: %d', [mouseCell.X, mouseCell.Y]);
end;

procedure TMainForm.dbgLinkCellClick(Column: TColumn);
begin
  if mouseCell.X = 2 then
  begin
    ShowMessage(Format('Coluna: %d', [mouseCell.X]));
  end;
end;

initialization
  ReportMemoryLeaksOnShutdown := true;

end.

source: link

The dbgLink.MouseCoord(X,Y); method returns the cell in coordinates also according to the position of the mouse passed by the OnMouseMove event.

    
15.03.2014 / 13:06