Link to home
Start Free TrialLog in
Avatar of ChLa
ChLaFlag for United States of America

asked on

Edit box color not working

I want to change an editbos's color at runtime to yellow to signal an out of norm setting. It isn't working. Is there something else I must set. This is my code:
   Edit3.Color := clYellow;
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

No, that is all you have to do, this technique works perfectly.
If it doesn't for you , then maybe you have something else going on that prevent the change of color

Please post your code
Avatar of jimyX
jimyX

While you are expecting the problem is from the line "Edit3.Color := clYellow;", you might be having unfulfilled condition or validation of Boolean values that lead to execute that line.
may depend on your os
Avatar of ChLa

ASKER

Note that I am using Delphi 2010 on a W7 laptop.
'
I made a simple App to demonstrate the problem.
Open Delphi
Add new MDI project
On the form put two panels and an edit.
Make both panels and the edit colors clCream at design time.
Add an on-click event for both panels.
Make the on-click for panel one set color of 2 panels and edit to clYellow
Make on-click event for panel two set all three colors back to clCream
When I click the first panel only the panels turn yellow. Edit stays cream.

Here is my code:


procedure TMainForm.Panel1Click(Sender: TObject);
begin
  Panel1.Color := clYellow;
  Panel2.Color := clYellow;
  Edit2.Color := clYellow;
end;

procedure TMainForm.Panel2Click(Sender: TObject);
begin
  Panel1.Color := clCream;
  Panel2.Color := clCream;
  Edit2.Color := clCream;
end;

I didn't touch or change anything else.
I see ! There is a problem with the panels color when you have the themes activated, they always look "transparent" whatever the color they have.
And as your TEdit have always the same color as it's parent, it will probably not change the color either.

1) try disabling the themes and see if that is doing now what you wish
2) I will (later tonight) figure it out with Delphi XE
Well, I am please to announce that in Delphi XE, those annoying problems with panels color with themes enabled have been fixed.

I have done exactly what you said and it works... Test it on your system, just to be sure
MDI.zip
Avatar of ChLa

ASKER

I found a place for themes in the Windows>controlPanel>Allcontrol Panel Items>Personalization.
The installed theme is Dell. Is this the active theme ? Should I remove it ?
There are seven Aero Themes, six basic and high contrast themes, and no My Themes.
I think the only thing I ever did to display settings was to select a desktop background slide show.
Avatar of ChLa

ASKER

I ran the exe file from the MDI.zip
The border of the box is changing color, but not the background where the test is.In my app I am using panels as a label, and the edit also as a window, but with input.
I have only very narrow bevel on the three boxes, and the bevel is lowered. In this condition, the panels look like the edit box. I do not really see the borders and so didn't know they could look like they do in your app. I wouldn't know if they were changing color.
The panels in my app do change color where the test is. I am hoping to get the background where the test is, the white area, to turn yellow. It doesn't in your app.
I just checked...
If I set the text box to yellow at design time, the inside area does turn yellow. But when I run it it is white again.
Avatar of ChLa

ASKER

FYI...I took the exe of my tiuny test program and tried it on some other flavors of Windows. I tried it on three XP machines, a Vista, and a W2K computer. The edit box never turned yellow !

You have to set the ParentBackground to False
procedure TMainForm.Panel1Click(Sender: TObject);
begin
  Panel1.ParentBackground := False;
  Panel1.Color := clYellow;
  
  Panel2.ParentBackground := False;
  Panel2.Color := clYellow;

  Edit2.Color := clYellow;
end;

Open in new window

No need for that
When you change the Color property, ParentColor is AUTOMATICALLY changed to False
He is experiencing a well known bug with panels. I didn't knew it impacted Edit box as well.
Thankfully that has been fixed in XE, it was time ! But ChLa have 2010 and we'll have to find a fix for him
Avatar of ChLa

ASKER

I was just working with a Memo box. At design time it turns completely yellow. At run time it turns partly yellow. Horizontally where there is text stays white. Horizontally where there is no text turns yellow. It becomes striped. It does the same with runtime changes.

I report this in case it is a clue, not because it is something I need fixed. The edit box fix will be useful though.

I am using the memo box to report things like when a long procedure is working, and when it is done. I hae it turn yellow when it reports "Working...Pleaas stand by", and back to white when it reports "Done"

has anybody had a look at the latest delphi source code ?
since windows 3.1 (yes that's a decade ago) i never seen so many checks for the os
and the different behaviour based on the type of os

look for CheckWin32Version(6)
and the comments borland/embarcadero put in are hilarious


     // A pressed "flat" speed button has white text in XP, but the Themes
      // API won't render it as such, so we need to hack it.
      if (ToolButton <> ttbToolbarDontCare) and not CheckWin32Version(6) then
        Canvas.Font.Color := clHighlightText
      else
        if FFlat then
          Offset := Point(1, 0);
 
see ... even they don't have the ultimate piece of code to solve all theme problems
thx to microsoft
I remember loosing some hairs the first time I ran one of my applications under Vista.... and it took me 2 days to completely figure out a hack to have the correct colors of panels, tabSheets and labels both With and Without themes activated, under both XP and Vista... at one moment I thought there would always be a case where something would not render correctly if I fixed another case.
By the look of it, Delphi XE has solved many issues already. I only have it since 3 month, and haven't had to create a seriously complex color-coded application, so I only found out for this question ! Good news for my remaining hairs !
you may want to try the memory patch/fixes for delphi here:
http://andy.jgknet.de/blog/ide-tools/ide-fix-pack-2009-10/
Try this :
procedure  TMainForm.FormCreate(Sender: TObject);
begin
 With Edit2 do ControlStyle:=ControlStyle - [csParentBackground] + [csOpaque];
end;

Open in new window

about what I said earlier after ewangoya comment on Panels.ParentBackground
No need for that
When you change the Color property, ParentColor is AUTOMATICALLY changed to False
My apologies, what he said is true and that solution applies to panels. I confused ParentBackground and ParentColor. I remember that being enough to fix the panels colors with Delphi 2007, I don't know if that is enough to fix also the Edit boxes as well, with Delphi 2010. If that is not the case, then forcing the edit ControlStyle to opaque (and whatever recalcitrant component regarding these color problems) has to do the trick.
Avatar of ChLa

ASKER

With Edit2 do ControlStyle:=ControlStyle - [csParentBackground] + [csOpaque];
end;

I just tried this...didn't work.
Using Delphi 2010 on Windows 7, I tried all available themes and was able to change the color for the TPanel and TEdit with no problems
unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm3 = class(TForm)
    Edit1: TEdit;
    Panel1: TPanel;
    Panel2: TPanel;
    Edit2: TEdit;
    procedure Panel1Click(Sender: TObject);
    procedure Panel2Click(Sender: TObject);
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

procedure TForm3.Panel1Click(Sender: TObject);
begin
  Panel1.ParentBackground := False;
  Panel1.Color := clYellow;
  Edit1.Color := clYellow;

  Panel2.ParentBackground := False;
  Panel2.Color := clYellow;
  Edit2.Color := clYellow;
end;

procedure TForm3.Panel2Click(Sender: TObject);
begin
  Panel2.ParentBackground := False;
  Panel2.Color := clCream;
  Edit2.Color := clCream;

  Panel1.ParentBackground := False;
  Panel1.Color := clCream;
  Edit1.Color := clCream;
end;

end.

Open in new window

Yes, me too I had no problem at all, with XE though. Maybe there is something wrong in your system.
Can you provide your exe (rename it .txt so that ExEx file upload does not block it), and maybe your code ?
That way we can test, and see if that is an application problem (exe not working here either), a system problem (app working great), or a delphi problem (exe not working, but source recompiled is working)
Otherwise we are in the dark here.
Avatar of ChLa

ASKER

I tried adding the line Panel1.ParentBackground := False; to my simple sample program shown in the fourth comment box above.
I implemented it exactly as in the sample code from ewangoya two comments above.
It made no difference. Edit box stayed white.

I then tried adding the line With Edit2 do ControlStyle:=ControlStyle - [csParentBackground] + [csOpaque]; just to see if they would somehow work together. Nope. Edit 2 stays white.

Is Delphi XE Delphi 2011 ?  I could upgrade.  

The comment above from epasquier seems to have been menat for someone else.

nope, the comment was for you. Yes XE=2011
please post your exe here, but since ExEx will not allow posting of exe, rename it with another accepted extension (txt for example)
Avatar of ChLa

ASKER

OK,
You want the exe from the simple demo program...right ? Do you want it simple, or with the lines added that make ParentBackground False ?
the thing is that ParentBackground = False is only useful for the panel color. The pb with TEdit is a complete mystery. Include it anyway.

One thing I'm not too sure : you do have solved your problem with the panel, right ? with ParentBackground ? or that one too is to put in the mystery list of why nothing works for you ?
Avatar of ChLa

ASKER

Only the edit and memo have a problem. All the other things work. Panels change color as expected.

Here is the source for the simple program as it exists. I have rem'd out the things I tried that didn't work. I will send the .exe in just a minute
unit MAIN;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, Menus,
  StdCtrls, Dialogs, Buttons, Messages, ExtCtrls, ComCtrls, StdActns,
  ActnList, ToolWin, ImgList;

type
  TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    FileNewItem: TMenuItem;
    FileOpenItem: TMenuItem;
    FileCloseItem: TMenuItem;
    Window1: TMenuItem;
    Help1: TMenuItem;
    N1: TMenuItem;
    FileExitItem: TMenuItem;
    WindowCascadeItem: TMenuItem;
    WindowTileItem: TMenuItem;
    WindowArrangeItem: TMenuItem;
    HelpAboutItem: TMenuItem;
    OpenDialog: TOpenDialog;
    FileSaveItem: TMenuItem;
    FileSaveAsItem: TMenuItem;
    Edit1: TMenuItem;
    CutItem: TMenuItem;
    CopyItem: TMenuItem;
    PasteItem: TMenuItem;
    WindowMinimizeItem: TMenuItem;
    StatusBar: TStatusBar;
    ActionList1: TActionList;
    EditCut1: TEditCut;
    EditCopy1: TEditCopy;
    EditPaste1: TEditPaste;
    FileNew1: TAction;
    FileSave1: TAction;
    FileExit1: TAction;
    FileOpen1: TAction;
    FileSaveAs1: TAction;
    WindowCascade1: TWindowCascade;
    WindowTileHorizontal1: TWindowTileHorizontal;
    WindowArrangeAll1: TWindowArrange;
    WindowMinimizeAll1: TWindowMinimizeAll;
    HelpAbout1: TAction;
    FileClose1: TWindowClose;
    WindowTileVertical1: TWindowTileVertical;
    WindowTileItem2: TMenuItem;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton9: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    ToolButton10: TToolButton;
    ToolButton11: TToolButton;
    ImageList1: TImageList;
    Panel1: TPanel;
    Panel2: TPanel;
    Edit2: TEdit;
    procedure FileNew1Execute(Sender: TObject);
    procedure FileOpen1Execute(Sender: TObject);
    procedure HelpAbout1Execute(Sender: TObject);
    procedure FileExit1Execute(Sender: TObject);
    procedure Panel1Click(Sender: TObject);
    procedure Panel2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure CreateMDIChild(const Name: string);
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

uses CHILDWIN, about;

procedure TMainForm.CreateMDIChild(const Name: string);
var
  Child: TMDIChild;
begin
  { create a new MDI child window }
  Child := TMDIChild.Create(Application);
  Child.Caption := Name;
  if FileExists(Name) then Child.Memo1.Lines.LoadFromFile(Name);
end;

procedure TMainForm.FileNew1Execute(Sender: TObject);
begin
  CreateMDIChild('NONAME' + IntToStr(MDIChildCount + 1));
end;

procedure TMainForm.FileOpen1Execute(Sender: TObject);
begin
  if OpenDialog.Execute then
    CreateMDIChild(OpenDialog.FileName);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  //With Edit2 do ControlStyle:=ControlStyle - [csParentBackground] + [csOpaque];
end;

procedure TMainForm.HelpAbout1Execute(Sender: TObject);
begin
  AboutBox.ShowModal;
end;

procedure TMainForm.Panel1Click(Sender: TObject);
begin
  //Panel1.ParentBackground := False;
  Panel1.Color := clYellow;

  //Panel2.ParentBackground := False;
  Panel2.Color := clYellow;
  Edit2.Color := clYellow;
end;

procedure TMainForm.Panel2Click(Sender: TObject);
begin
  Panel1.Color := clCream;
  Panel2.Color := clCream;
  Edit2.Color := clCream;
end;

procedure TMainForm.FileExit1Execute(Sender: TObject);
begin
  Close;
end;

end.

Open in new window

Avatar of ChLa

ASKER

Here I have attached the .exe, after removing the extension. MDIAPP MDIAPP
Avatar of ChLa

ASKER

I don't now why it attached it twice. Remember, I tried taking this simple exe to a couple of other computers as described in a comment above, and the same happened, no color for the edit.
Avatar of ChLa

ASKER

Quicl unrelated question...sorry. What do i change so the exe has the same small window I see in form view ? I tried making auto size false, and also auto scale.
Ok, same problem here.

And I tested again my own sample I gave you, as this one was working. I found a major difference : your TEdit is directly on the main window surface, where mine is on one of the panels.
So I moved mine, and guess what : I have the same problem as you, with Delphi XE. Ah Ah !!

Now we are moving forward !

So, we could spend the rest of the night trying to figure why a TEdit on the client area of a main window of an MDI application refuses to change its color, or you could put it on a TPanel which as Autosize=True and no borders or bevel. That looks the same except it works !
Avatar of ChLa

ASKER

I used to always put everything on panels. It seemed to be more important on older versions of Delphi. With Delphi 2010 I have been putting things directly onto the main form. It seems a bit less cluttered. Also, I have had problems with such things as trying to move a control off a panel.

It is easy to add a panel to this small test app. A bit more challenging with my larger work as it already has a lot of controls on it.

On the simple test app, I just added a panel and moved the edit to it. The edit still doesn't change color !
I then tried changing the new panel color to yellow before changing the edit on it to yellow. The edit still stays white on this computer.
Now that is annoying... I really thought we got that one. So we will try the same method that get us there : send me your app compiled with D2010 with TEdit on a panel , which does not work for you either.
Post the full code as well : .DPR , all .PAS & .DFM in a ZIP archive
I have read again your response to my initial posted test application, where you say that the border of the Edit change color but not the background. That response I had difficulties to understand the first time I read, because you mistyped "test" instead of "text" almost all over. Now with that "translation" I understand it better, but still don't see why you would have the border changing colors.
Still, I understand that you have tried my app on your system, and you tried yours on many other. Can you try mine on other stations, OS etc .. ? As I'm certain it works for me on Seven and XP, with standard themes, I would like to be sure of the result of my app in your other environments.
If you could provide some snapshots of the apps in the environments that do not work, that could also help.
Avatar of ChLa

ASKER

Hi,

I will send you the things you asked for as soon as I can.
When I said the border changed color, I had run and tested the exe, but not looked at the code. Now I realize what I thought might have been a border, was actually a panel behind the edit. It changed colors, the edit didn't. Sorry for the mispellings.
Avatar of ChLa

ASKER

Here is the .exe. I removed the extension.
MDIAPP
Avatar of ChLa

ASKER

Here is the source code
unit MAIN;

interface

uses Windows, SysUtils, Classes, Graphics, Forms, Controls, Menus,
  StdCtrls, Dialogs, Buttons, Messages, ExtCtrls, ComCtrls, StdActns,
  ActnList, ToolWin, ImgList;

type
  TMainForm = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    FileNewItem: TMenuItem;
    FileOpenItem: TMenuItem;
    FileCloseItem: TMenuItem;
    Window1: TMenuItem;
    Help1: TMenuItem;
    N1: TMenuItem;
    FileExitItem: TMenuItem;
    WindowCascadeItem: TMenuItem;
    WindowTileItem: TMenuItem;
    WindowArrangeItem: TMenuItem;
    HelpAboutItem: TMenuItem;
    OpenDialog: TOpenDialog;
    FileSaveItem: TMenuItem;
    FileSaveAsItem: TMenuItem;
    Edit1: TMenuItem;
    CutItem: TMenuItem;
    CopyItem: TMenuItem;
    PasteItem: TMenuItem;
    WindowMinimizeItem: TMenuItem;
    StatusBar: TStatusBar;
    ActionList1: TActionList;
    EditCut1: TEditCut;
    EditCopy1: TEditCopy;
    EditPaste1: TEditPaste;
    FileNew1: TAction;
    FileSave1: TAction;
    FileExit1: TAction;
    FileOpen1: TAction;
    FileSaveAs1: TAction;
    WindowCascade1: TWindowCascade;
    WindowTileHorizontal1: TWindowTileHorizontal;
    WindowArrangeAll1: TWindowArrange;
    WindowMinimizeAll1: TWindowMinimizeAll;
    HelpAbout1: TAction;
    FileClose1: TWindowClose;
    WindowTileVertical1: TWindowTileVertical;
    WindowTileItem2: TMenuItem;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton9: TToolButton;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    ToolButton10: TToolButton;
    ToolButton11: TToolButton;
    ImageList1: TImageList;
    Panel1: TPanel;
    Panel2: TPanel;
    Edit2: TEdit;
    Panel3: TPanel;
    procedure FileNew1Execute(Sender: TObject);
    procedure FileOpen1Execute(Sender: TObject);
    procedure HelpAbout1Execute(Sender: TObject);
    procedure FileExit1Execute(Sender: TObject);
    procedure Panel1Click(Sender: TObject);
    procedure Panel2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure CreateMDIChild(const Name: string);
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

uses CHILDWIN, about;

procedure TMainForm.CreateMDIChild(const Name: string);
var
  Child: TMDIChild;
begin
  { create a new MDI child window }
  Child := TMDIChild.Create(Application);
  Child.Caption := Name;
  if FileExists(Name) then Child.Memo1.Lines.LoadFromFile(Name);
end;

procedure TMainForm.FileNew1Execute(Sender: TObject);
begin
  CreateMDIChild('NONAME' + IntToStr(MDIChildCount + 1));
end;

procedure TMainForm.FileOpen1Execute(Sender: TObject);
begin
  if OpenDialog.Execute then
    CreateMDIChild(OpenDialog.FileName);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  //With Edit2 do ControlStyle:=ControlStyle - [csParentBackground] + [csOpaque];
end;

procedure TMainForm.HelpAbout1Execute(Sender: TObject);
begin
  AboutBox.ShowModal;
end;

procedure TMainForm.Panel1Click(Sender: TObject);
begin
  Panel1.Color := clYellow;
  Panel2.Color := clYellow;
  Panel3.Color := clYellow;
  Edit2.Color := clYellow;
end;

procedure TMainForm.Panel2Click(Sender: TObject);
begin
  Panel1.Color := clCream;
  Panel2.Color := clCream;
  Panel3.Color := clCream;
  Edit2.Color := clCream;
end;

procedure TMainForm.FileExit1Execute(Sender: TObject);
begin
  Close;
end;

end.

Open in new window

Avatar of ChLa

ASKER

Here is the ,dfm file
Main.dfm
Avatar of ChLa

ASKER

here is the .dpr file
MDIAPP.dpr
ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ChLa

ASKER

That did do it. Amazing. I also tried it with a TMemo. It worked for it also. First I put the memo on the form next to a new panel. It only partly turned yellow. Then I dragged it onto a panel. Still only part yellow. Then I cut it and then pasted it back onto the now-selected panel. Now it fully turns yellow. Sorry for all the trouble.
Avatar of ChLa

ASKER

Thank you.