Recent

Author Topic: CustomEdit: How do you set the Font Color?  (Read 1047 times)

Aruna

  • Hero Member
  • *****
  • Posts: 809
CustomEdit: How do you set the Font Color?
« on: September 26, 2024, 04:16:37 am »
Hi, I’ve created a custom text input box that functions correctly, but I’m running into an issue with setting the text color. When I attempt to set the color programmatically, it doesn’t take effect. However, if I set the color in the Object Inspector for the Main Form, it applies as expected. How can I fix this? I’ve attached the source code in a zip file, and the screenshot shows the login form where I’m trying to change the text color.


Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
Re: CustomEdit: How do you set the Font Color?
« Reply #1 on: September 26, 2024, 06:42:05 am »
You just need to call invalidate after you change the color in code.
That will force a repaint.
objects are fine constructs. You can even initialize them with constructors.

jeremiah

  • New Member
  • *
  • Posts: 23
Re: CustomEdit: How do you set the Font Color?
« Reply #2 on: September 26, 2024, 06:44:30 am »
On button.click set font color:

MyEdit.Font.Color := clred;  // Background color

peace...

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: CustomEdit: How do you set the Font Color?
« Reply #3 on: September 26, 2024, 07:37:05 am »
changing the color (alone) is not going to work for gtk and certain themes in use. issue 38516

having said that:
Code: Pascal  [Select][+][-]
  1. procedure TCustomEdit2.Paint;
  2. begin
  3.   // Draw the TEdit background color
  4.   Canvas.Brush.Color := $00EF3F0E;
  5. // Canvas.Brush.Color := $0063D0E5;
  6.   Canvas.FillRect(ClientRect);
  7.  
  8.   // Draw the TEdit Border
  9.   Canvas.Pen.Color:=clYellow;
  10.   Canvas.Pen.Width:=1;
  11.   Canvas.Rectangle(clientrect.Left-10,clientrect.Top-10,clientrect.Right,clientrect.Bottom);
  12.  
  13.   // Draw the background image if assigned
  14.   if Assigned(FBackgroundImage.Graphic) then
  15.     Canvas.Draw(0, 0, FBackgroundImage.Graphic);
  16.  
  17.   // Draw the text
  18.   Form1.Font.Color:=clBlack;
  19. //  Canvas.Font.Color:=clWhite;
  20.   Canvas.Font.Color:=clRed;
  21.   Canvas.Font.Assign(Self.Font);  // Use the font of the control
  22.   Canvas.TextOut(15, (Height - Canvas.TextHeight(FText)) div 2, FText);  // Center text vertically
  23.   invalidate;
  24. end;
  25.  

this ?
Code: Pascal  [Select][+][-]
  1. procedure TCustomEdit2.Paint;
  2. begin
  3.   // Draw the TEdit background color
  4.   Canvas.Brush.Color := FBackgroundColor;
  5.   Canvas.FillRect(ClientRect);
  6.  
  7.   // Draw the TEdit Border
  8.   Canvas.Pen.Color:=clYellow;
  9.   Canvas.Pen.Width:=1;
  10.   Canvas.Rectangle(clientrect.Left-10,clientrect.Top-10,clientrect.Right,clientrect.Bottom);
  11.  
  12.   // Draw the background image if assigned
  13.   if Assigned(FBackgroundImage.Graphic) then
  14.     Canvas.Draw(0, 0, FBackgroundImage.Graphic);
  15.  
  16.   // Draw the text
  17.   Self.Font.Color := clRed;  // <-- or set it in mainform.myedit.font.color (as per jeremiah)
  18.   Canvas.Font.Assign(Self.Font);  // Use the font of the control
  19.   Canvas.Font.Color:=clRed; // <-- or set the canvas font color after the assign
  20.   Canvas.TextOut(15, (Height - Canvas.TextHeight(FText)) div 2, FText);  // Center text vertically
  21.   invalidate;
  22. end;
  23.  
  24.  
« Last Edit: September 26, 2024, 08:56:04 am by TRon »
Today is tomorrow's yesterday.

 

TinyPortal © 2005-2018