Hyperlink in a Dbgrid field

Asked

Viewed 713 times

4

I am making a small internal software to research extensions in my company. In addition to the extensions I also put an email field in the database as can be seen below:

inserir a descrição da imagem aqui

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

In my searches I have not yet found any way to do. So I thought of using a TLabel inside the field. I can put the ShellExecute at the event OnClick of TLabel and also change its cursor icon.

If Tlabel is a good solution, how to insert Tlabel into one-column cells in Dbgrid?

Or what another good solution would be?

  • I completely rephrased the question to make my goal clearer.

  • Would you have any problem using the event onCellClick tdbgrid?

  • Dear @Danielgrillo, could post how you solved the problem? Grateful!

1 answer

2

Look at 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: http://drgarcia1986.wordpress.com/2013/01/17/dicas-sobre-o-componente-dbgrid-do-delphi/

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.