Recent

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

pcurtis

  • Hero Member
  • *****
  • Posts: 517
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
Laz 2.0.10
FPC 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 4376
Re: Custom control in DBGrid
« Reply #1 on: February 22, 2021, 10:49:33 pm »
You most likely want to deal with the OnSelEditorEvent.

You can direct a control of your own to show there because if gives the option of replacing the control of choice but if needs to be a windows to control or a control that is based from it. I don't think a Tlabel will do it but a TPanel will and it has a caption with mouse and input options
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 3790
Re: Custom control in DBGrid
« Reply #2 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: 517
Re: Custom control in DBGrid
« Reply #3 on: February 23, 2021, 07:22:35 am »
I only want to display not edit
Windows 10
Laz 2.0.10
FPC 3.2.0

lucamar

  • Hero Member
  • *****
  • Posts: 3790
Re: Custom control in DBGrid
« Reply #4 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: 517
Re: Custom control in DBGrid
« Reply #5 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
Laz 2.0.10
FPC 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 4376
Re: Custom control in DBGrid
« Reply #6 on: February 25, 2021, 12:09:28 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1SelectEditor(Sender: TObject; aCol, aRow: Integer;
  2.   var Editor: TWinControl);
  3. begin
  4.   If (aCol =1)and(ARow=1) Then Editor := Nil;
  5. end;                                                              
  6.  

I think what you are looking for is simply to cancel the editor in that cell so it only displays and responses to clicks?
The above code will now allow editing in a specific cell but it will still response to what ever else..

The only true wisdom is knowing you know nothing

pcurtis

  • Hero Member
  • *****
  • Posts: 517
Re: Custom control in DBGrid
« Reply #7 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
Laz 2.0.10
FPC 3.2.0

pcurtis

  • Hero Member
  • *****
  • Posts: 517
Re: Custom control in DBGrid
« Reply #8 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
Laz 2.0.10
FPC 3.2.0

jamie

  • Hero Member
  • *****
  • Posts: 4376
Re: Custom control in DBGrid
« Reply #9 on: February 25, 2021, 10:47:45 pm »
why not try to cast the existing editor that comes in with the OnSelectEditor to a TEDIT and then set the ReadOnly := true;;


 For example

 within the event..

Code: Pascal  [Select][+][-]
  1. if Editor <> Nil Then
  2.     TEdit(editor).ReadOnly := Not (AreWeATheColumnOfChoice=CoumnNumber);
  3.  

Just an idea I guess but this will fix the Editor so that its read only and it only displays data, does not edit..
The only true wisdom is knowing you know nothing

pcurtis

  • Hero Member
  • *****
  • Posts: 517
Re: Custom control in DBGrid
« Reply #10 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
Laz 2.0.10
FPC 3.2.0

 

TinyPortal © 2005-2018