Topic: Change Textbox background when Read Only

Is it possible to change the background of a Textbox when set to Read Only?

I have some textbox which become read only on certain conditions and I would like to show that these textboxes are actually not editable. When back to Read And Write, the Textbox background should go back to default.

Any hint?

Re: Change Textbox background when Read Only

Yes.

Default color

Form1.Edit1.Color :=clWindow;

gray color

Form1.Edit1.Color :=clBtnFace;
Dmitry.

Re: Change Textbox background when Read Only

Thanks for the hint Dmitry, however

Form1.Edit1.Color :=clBtnFace; 

does not work, I had to replace clBtnFace by $00CCCCCC

Here is how I implemented it:

if AddInfo.AddPos.ReadOnly then AddInfo.AddPos.Color :=$00CCCCCC else AddInfo.AddPos.Color :=clWindow; //grey out the field when read only

Re: Change Textbox background when Read Only

Is there a way to possibly set 2 status on a condition without multiplying the number of script lines?

For instance I would like to set a field to read only if content <>0 AND being grayed out.

I managed to set the textbox to read only, how to set the background to grey (color := $003333FF) on same code line ?


Current code is:

if Admin.Edit_typ.Text<>'' then Admin.Edit_typ.ReadOnly := True else Admin.Edit_typ.ReadOnly := False;

I tried to add

AND Admin.Edit_typ.color := $003333FF 

before

else 

without success. sad

Re: Change Textbox background when Read Only

try this

if Admin.Edit_typ.Text<>'' then
Admin.Edit_typ.ReadOnly := True;
Admin.Edit_typ.color := $003333FF;
else
Admin.Edit_typ.ReadOnly := False;

Re: Change Textbox background when Read Only

It does not work, it is expecting a ";" after the color code forever which already exists

Re: Change Textbox background when Read Only

You should use begin end keywords

if Admin.Edit_typ.Text<>'' then 
begin
   Admin.Edit_typ.ReadOnly := True;
   Admin.Edit_typ.color := $003333FF;
end
else
begin
   Admin.Edit_typ.ReadOnly := False;
   Admin.Edit_typ.color := clWindow;
end;
Dmitry.

Re: Change Textbox background when Read Only

Thank you Dmitry,


it is all about syntax. I thought we could use AND clause between if and else but it is not the case.

It works.