Recent

Author Topic: Custom control in StringGrid  (Read 1607 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Custom control in StringGrid
« on: February 22, 2021, 07:55:59 pm »
Hi All,
how can I draw a custom component in a StringGrid cell and take the cells value e.g. a TLabel, and handle its click event?

Eventually it will be a hyperlink.

I do not want to handle the cell oncick event.

Thanks in advance.
« Last Edit: February 27, 2021, 09:15:38 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Custom control in DBGrid
« Reply #1 on: February 23, 2021, 12:10:34 am »
I don't think a Tlabel will do it but a TPanel will and it has a caption with mouse and input options

The proper replacement for a TLabel when one needs a TWinControl (with its associated handle, input events, etc.) is TStaticText.

A panel is not quite the same.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Custom control in DBGrid
« Reply #2 on: February 23, 2021, 07:22:35 am »
I only want to display not edit
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Custom control in DBGrid
« Reply #3 on: February 23, 2021, 07:18:02 pm »
I only want to display not edit

That's why we are suggesting "static* text" controls instead of, say, a TEdit ;)


* "Static" as in "not user-editable"; you can change the contents in code whenever you see fit through the Caption property :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Custom control in DBGrid
« Reply #4 on: February 24, 2021, 06:12:33 am »
But how to do it?

Any code?

I have attached a small test program and see the first problem.
The program displays a grid with two columns x and y.

The event OnSelectEditor looks like this

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1SelectEditor(Sender: TObject; Column: TColumn; var Editor: TWinControl);
  2. begin
  3.   if Column.FieldName = 'y' then
  4.     ShowMessage(Column.FieldName);
  5. end;
  6.  

When the program is first run ShowMessage is never displayed.
Once the grid is clicked all works OK.
Whats wrong.
Also how do I create display the StaticText control?

My idea of how it should work would be something like this (obviously just an idea):

Code: Pascal  [Select][+][-]
  1.     procedure TForm1.DBGrid1SelectEditor(Sender: TObject; Column: TColumn; var Editor: TWinControl);
  2.     begin
  3.       if Column.FieldName = 'y' then
  4.         begin
  5.           Editor := TStaticText.Create;
  6.           Editor.OnClick := @OnClickEvent;
  7.           Editor.Caption := // text to display;
  8.         end;  
  9.     end;
  10.      
  11.  

Thanks
« Last Edit: February 24, 2021, 10:01:04 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Custom control in DBGrid
« Reply #5 on: February 25, 2021, 05:07:41 am »
What does that do? DBGrid has no aRow, aCol.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1SelectEditor(Sender: TObject; Column: TColumn; var Editor: TWinControl);
  2. begin
  3.   Editor:=nil
  4. end;
  5.  

Does nothing.
Anyway, how does that solve the original issue?

Quote
how can I draw a custom component in a DBGrid cell?
« Last Edit: February 25, 2021, 05:19:53 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Custom control in DBGrid
« Reply #6 on: February 25, 2021, 10:29:12 pm »
Something like

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer;
  Column: TColumn; State: TGridDrawState);
begin
  if Column.DesignIndex = 0 then
    begin
      StaticTexr1.SetBounds(Rect.Left, Rect.Top, Rect.Width, Rect.Height);
    end;

  TDBGrid(Sender).DefaultDrawColumnCell(Rect,DataCol,Column,State);
end;

but it doesn't work.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Custom control in DBGrid
« Reply #7 on: February 26, 2021, 06:50:31 am »
This is going nowhere.
If it makes life easier I will change from DBGrid to StringGrid
I only want to display the text from a specified column in a control (for example a TLabel) on the grid. The control must must be displayed on all rows, not just the selected row.

All I want to do is display the row / cell text as a hyperlink, so when the mouse is over the text it changes to crHandPoint, and when I click the text (not the cell) it opens the hyperlink in the associated program.
« Last Edit: February 27, 2021, 09:18:09 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018