Recent

Author Topic: How to change edit color (font or background) of a stringgrid cell  (Read 3094 times)

aducom

  • Full Member
  • ***
  • Posts: 162
    • http://www.aducom.com
I have a master form in a dark style (black background, white font). When I add an edit field and set parent color/font of the field then is is shown with a white font and black background. If I do the same with a stringgrid then the grid is black with white lines/text. But when I edit a cell, then the background becomes white, with a white font... How can I change this? (background and/or font?)

Nicole

  • Hero Member
  • *****
  • Posts: 1277
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #1 on: October 12, 2022, 12:18:06 pm »
Do you work with the even OnDrawCell?

Zvoni

  • Hero Member
  • *****
  • Posts: 3165
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #2 on: October 12, 2022, 12:44:45 pm »
As nicole implied: Must be ownerdrawn.
Would have to look in my codes, since i know i've done something similar
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

loaded

  • Hero Member
  • *****
  • Posts: 876
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #3 on: October 12, 2022, 12:50:47 pm »
I was using it on Dbgrid. I tried now, it works on Stringgrid too.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  2.   aState: TGridDrawState);
  3. begin
  4.    if (acol=3) and (arow=3) then
  5.    begin
  6.    (Sender as TStringGrid).Canvas.Brush.Color:=clYellow;
  7.    (Sender as TStringGrid).Canvas.Font.Color:=clBlack;
  8.    end;
  9. end;  
« Last Edit: October 12, 2022, 12:52:55 pm by loaded »
The more memory computers have, the less memory people seem to use. 😅

Bart

  • Hero Member
  • *****
  • Posts: 5642
    • Bart en Mariska's Webstek
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #4 on: October 12, 2022, 01:57:04 pm »
@loaded: does that work whiles editing? I.o.w.: does it also affect the color of the inplace editor?

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5642
    • Bart en Mariska's Webstek
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #5 on: October 12, 2022, 02:17:34 pm »
You might have to set the ParentColor and ParentFont properties of the Editor to True.
I tested this changing the constructor for TStringCellEditor and the editor now has the same color and fontcolor as the grid.

Bart

loaded

  • Hero Member
  • *****
  • Posts: 876
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #6 on: October 12, 2022, 03:13:19 pm »
@loaded: does that work whiles editing? I.o.w.: does it also affect the color of the inplace editor?
Unfortunately, it doesn't work while editing. Sorry.

The more memory computers have, the less memory people seem to use. 😅

Nicole

  • Hero Member
  • *****
  • Posts: 1277
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #7 on: October 12, 2022, 03:33:59 pm »
How owner draw works:
Every time your StringGrid is filled, scrolle, recalculated, the pixels of the control are re-drawn.

This means several things:
- it will not work at design time
- if you color a cell, the content is "gone" (overdrawn), so you have to draw it again as well.

- there is a property, you have to set to true, that it works at all, I think it was "ownerdrawn" or so.

You are discouraged now?
Do not worry, here is a method for DBGrid, you can adjust.
Just search this property, set it to true.
And then click the method and paste the code below into it.
Comment those lines, which will not work for TStringGrid (because they are for TDBGrid).

It does it all
- comment how to draw where
- re-draw the fonts
- calculate some things
- many things you will not need (everything with query), just delete them.

Enjoy


Code: Pascal  [Select][+][-]
  1. procedure TFrame_MeinTag.DBGrid_einTagDrawColumnCell(Sender: TObject;
  2.   const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
  3. var farbe: TColor;
  4.     s: string;
  5. begin
  6. // wird jede Zelle aufgerufen, dabei liegen dieParameter so:
  7. ///  IBQuery_Alltag.RecNo 1 1 1 1
  8. ///                       2 2 2 2
  9. ///  column.index 0 1 2 3
  10. ///        0 1 2 3
  11. ///        0 1 2 3
  12. ///  columns 0 1 2 3
  13. ///         0 1 2 3
  14. ///        0 1 2 3
  15. /// just draw here, no content change
  16.  
  17.   with (Sender as TDBGrid) do begin
  18.  
  19.     if DataCol=0 then begin    // das soll nicht JEDE Spalte getan werden
  20.        if DataSource_EinTag.DataSet.RecNo=1
  21.          then comm_temp:= 'keine';
  22.        s:=DataSource_EinTag.DataSet.FindField('COMM').AsAnsiString;
  23.        s:=Trim(s);
  24.        faerbe_zeile:=9999;
  25.        if s <> comm_temp then begin // sucht, ob eine neue Ware in einer Zeile
  26.          comm_temp:=s;
  27.          faerbe_zeile:=ADQuery_einTag.RecNo;  // diese Zeile wird gefärbt
  28.                             end; end; // Ende der Waren-Suchschleife DataCol=0
  29.  
  30.        // jetzt kommt das Färben selbst
  31.       if faerbe_zeile=ADQuery_einTag.RecNo
  32.         then farbe := $00E7F2FF // cremefarben, eine Zeile
  33.         else farbe := clWhite;
  34.  
  35.       if (gdSelected in State) then Canvas.Brush.Color := clblue;
  36.       // das färbt jetzt nicht mehr
  37.       // einmal belassen, die Cremefarbe ist wichtiger
  38.  
  39.       Canvas.Brush.Color := farbe;   // Font-Farbe immer schwarz
  40.       Canvas.Font.Color := clBlack;
  41.  
  42.     Canvas.FillRect(Rect);                 // Rechteck malen und Text schreiben
  43.  
  44.     if Column.Field <> nil then begin
  45.       s:=Column.Field.AsString;
  46.       Canvas.TextOut(Rect.Left + 2, Rect.Top + 1,s);
  47.     end;
  48. // TextRect(Rect: TRect; X, Y: Integer; const Text: string);
  49. // wäre eine Alternative, allerdings wird der Text abgeschnitten, wenn er zu lange
  50.  
  51. //   If (Datacol = 18) then   // geht nicht für 17
  52. //     Canvas.TextOut(Rect.Left + 2, Rect.Top + 1,Column.Field.AsString);
  53. // theoretisch ginge auch das oben, aber nur für nicht calc-Felder
  54.  
  55.    end;  // zu (Sender as TDBGrid)
  56. end;

Nicole

  • Hero Member
  • *****
  • Posts: 1277
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #8 on: October 12, 2022, 03:37:01 pm »
to cleariify

Code: Pascal  [Select][+][-]
  1.     Canvas.FillRect(Rect);                 // colors your cell
  2.  
  3.     if Column.Field <> nil then begin  // fills in the overpainted text again
  4.       s:=Column.Field.AsString;
  5.       Canvas.TextOut(Rect.Left + 2, Rect.Top + 1,s);
  6.     end;

aducom

  • Full Member
  • ***
  • Posts: 162
    • http://www.aducom.com
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #9 on: October 12, 2022, 03:39:31 pm »
Thank you for your reactions. I know how I can paint (ownerdraw) the individual cells, but my issue is that I want to set the inline editor to use the parent color. If you edit the cell then the inplace editor takes over and it is white on white. @Bart, how did you do this? 'I tested this changing the constructor for TStringCellEditor and the editor now has the same color and fontcolor as the grid.' Is there an event of the StringGrid I can use? The grid is set to use parent color, but the inline editor does not use this.

Nicole

  • Hero Member
  • *****
  • Posts: 1277
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #10 on: October 12, 2022, 05:34:49 pm »
Ah, I see!
I had this problem as well and solved it. It was white on white as well.

Give me one or two days to find it. I am not sure, where I did it, but shall come across it soon.

wp

  • Hero Member
  • *****
  • Posts: 13264
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #11 on: October 12, 2022, 06:26:48 pm »
Nice idea, Bart. I had always thought that it is not possible to change the background color of a TEdit... I think there was a recent discussion about this (putting the Edit into an autosized Panel and setting the panel.Color as needed would allow to change the Edit.Color independend of the form's Color).

Is there an event of the StringGrid I can use? The grid is set to use parent color, but the inline editor does not use this.
Use the grid's OnSelectEditor event which allows to determine which kind of cell editor is to be used. Check whether the proposed editor is a TStringCellEditor and then set it's ParentColor to true. Note, however, that ParentColor is not a public property in TStringCellEditor; but you can cast it to a descendant class (TMyStringCellEditor in the code below) to get access to protected properties:
Code: Pascal  [Select][+][-]
  1. type
  2.   TMyStringCellEditor = class(TStringCellEditor)
  3.   public
  4.     property ParentColor;
  5.   end;
  6.  
  7. procedure TForm1.StringGrid1SelectEditor(Sender: TObject; aCol, aRow: Integer;
  8.   var Editor: TWinControl);
  9. begin
  10.   if (Editor is TStringcellEditor) then
  11.   begin
  12.     TMyStringCellEditor(Editor).ParentColor := true;
  13.   end;
  14. end;  

Bart

  • Hero Member
  • *****
  • Posts: 5642
    • Bart en Mariska's Webstek
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #12 on: October 12, 2022, 10:17:07 pm »
I changed the grids unit.
In the TStringCellEditor constructor I set ParentColor and ParentFont to True, just to see if that would solve the problem.
After that I set Grid.Color to clYellow and Grid.Font.Color to clRed: the StringEditor has the same colors now.

Ar first I thought DoEditorShow was the place to do it, but there I would need to cast the FEditor variable to the proper type.
In the long run, this is the proper place to implement it, I think.

I could imaging that we add an option to control this.

For the time being: override DoEditorShow (it's virtual) and cast FEditor into the proper type and set ParentColor and ParentFont to True.

Feel free to file a featyre request for this in the bugtracker, so that it won;t be forgotten.

Bart

aducom

  • Full Member
  • ***
  • Posts: 162
    • http://www.aducom.com
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #13 on: October 13, 2022, 09:48:55 pm »
Thank you for your answer Bart & wp. I will do a feature request. Meanwhile I'll follow your suggestions.
Regards, Albert
« Last Edit: October 13, 2022, 09:51:08 pm by aducom »

Bart

  • Hero Member
  • *****
  • Posts: 5642
    • Bart en Mariska's Webstek
Re: How to change edit color (font or background) of a stringgrid cell
« Reply #14 on: October 15, 2022, 04:16:55 pm »
I will do a feature request.

No need anymore.
I implemented such a feature in Lazarus main in #675ad022.
You can control it using goEditorParentFont and goEditorParentColor in the Options2 property.
(It'll work on the next time a cell editor is shown, not on an already open cell editor.)

Bart

 

TinyPortal © 2005-2018