Recent

Author Topic: A tiny detail about custom cell editors in a TStringGrid  (Read 397 times)

Al3

  • New Member
  • *
  • Posts: 28
A tiny detail about custom cell editors in a TStringGrid
« on: February 02, 2025, 03:16:40 pm »
Apologies for asking something that might be answered by inspecting sources, this is just my 4th Lazarus project, so I am still pretty new to Object Pascal.
It's so fun to work with it I am not giving enough care to my C projects :) ... :(

Because I cannot get the normal cell editor control font color to change (something makes it white, which is the same color as the background and therefore the text is invisible), I am providing a custom TEdit through OnSelectEditor. Lots of stuff must be handled manually it seems, but here is what I have thus far just in case someone needs it:
Code: Pascal  [Select][+][-]
  1. procedure TMyStringGrid.onMyResize (Sender: TObject);
  2. var
  3.     Grid:   TStringGrid;
  4.     CR:     TRect;
  5. begin
  6.     Grid := Sender as TStringGrid;
  7.     if not Assigned(Grid.Editor) then Exit;
  8.     CR := Grid.CellRect(Grid.Col, Grid.Row);
  9.     //TEdit(Grid.Editor).BoundsRect := Grid.CellRect(Grid.Col, Grid.Row);
  10.     TEdit(Grid.Editor).BoundsRect := Rect(CR.Left, CR.Top+3, CR.Right, CR.Bottom+3);
  11. end;
  12.  
  13. procedure TMyStringGrid.onMySelectEditor(Sender:        TObject;
  14.                                          aCol:          Integer;
  15.                                          aRow:          Integer;
  16.                                          var Edit:      TWinControl);
  17. var
  18.     Grid:   TStringGrid;
  19.     myEdit: TEdit;
  20.     CR:     TRect;
  21. begin
  22.  
  23.     (* Get Grid *)
  24.     Grid := Sender as TStringGrid;
  25.  
  26.  
  27.     (* Create Editor *)
  28.     myEdit := TEdit.Create(Sender as TComponent);
  29.     myEdit.Hide;
  30.  
  31.     CR              := Grid.CellRect(aCol, aRow);
  32.  
  33.     (* Configure Editor *)
  34.     myEdit.AutoSelect   := false; // TCDEdit does not need this
  35.     myEdit.Parent       := Sender as TWinControl;
  36.     myEdit.Text         := Grid.Cells[aCol, aRow];
  37.     myEdit.Alignment    := Grid.Columns.Items[aCol-1].Alignment;
  38.  
  39.     (* Alignment *)
  40.     //myEdit.BoundsRect := CR
  41.     myEdit.BoundsRect := Rect(CR.Left, CR.Top+3, CR.Right, CR.Bottom+3);
  42.  
  43.     (* Style *)
  44.     //myEdit.Brush.Color := clRed;
  45.     myEdit.Color := Grid.Columns[aCol-1].Color;
  46.     myEdit.Font.Color := clWhite;
  47.     myEdit.BorderStyle := bsNone;
  48.  
  49.     myEdit.CaretPos   := TPoint.Create(Length(myEdit.Text), 0);
  50.  
  51.     (* Assign editor *)
  52.     Edit := myEdit;
  53.     myEdit.Show;
  54. end;

The only problem I have is that the handler is signaled even with the goAlwaysShowEditor option not set.
If I don't attach a handler to this signal, the default edit appears after I focus and click/activate a particular cell, which is the expected behavior.
This means that some other wizardry is happening on the default edit control to behave like that, but I have no idea what.
Does it create some clickable canvas first?
What is the correct easiest way to do that?

Al3

  • New Member
  • *
  • Posts: 28
Re: A tiny detail about custom cell editors in a TStringGrid
« Reply #1 on: February 04, 2025, 08:26:08 am »
The default cell edit,
it's font color was overridden by the TStringGrid's parent font color.
I guess I'll stick to that.

 

TinyPortal © 2005-2018