Solved

How to change DBEdit read-only color?

Posted on 2001-06-03
12
1,205 Views
Last Modified: 2010-04-06
I have form with DBEdits. Some of them (or all) became read-only at runtime. How to change DBEdit color
(in read-only state at runtime) to color different than white nad back to normal when it's not read-only?
Changes occurs on Dataset state change.
0
Comment
Question by:siki
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6151336
Here is example I but in side a button so you can get an idea of how to change it base on the ReadOnly property.

procedure TForm1.Button1Click(Sender: TObject);
begin

  with DBEdit1 do begin
     ReadOnly := not ReadOnly;    
     If ReadOnly then begin
            Color := clBtnFace;
            Font.Color := clBtnText;
      end
       else begin
             Color := clWindow;
             Font.Color := clWindowText;
       end;

  end;  

end;
0
 

Expert Comment

by:Pandian012000
ID: 6151676
Hi

In your form show event try this

  if ded_alotdate.ReadOnly = true then
  begin
    ded_alotdate.Color := clred;
  end else
  begin
    ded_alotdate.Color := clwindow;
  end;


Regards
Pandian
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6151933
Hi Pandian
   sinisanenkov said:
>Changes occurs on Dataset state change.

 but what you post as answer will check first time when form show, and if the state changed again it will not change the color :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:CrazyOne
ID: 6152914
Yeah that was my understanding that the readonly would change at various times troughout the apps life.

Here is the same example from before but with some changes,

procedure TForm1.Button1Click(Sender: TObject);
begin

     DBEdit1.ReadOnly := not DBEdit1.ReadOnly;    
      SetDBEditColor(DBEdit1);    
 
end;

procedure TForm1.SetDBEditColor(Sender: TDBEdit);
begin

     If Sender.ReadOnly then begin
          Sender.Color := clBtnFace;
          Sender.Font.Color := clBtnText;
     end
     else begin
          Sender.Color := clWindow;
          Sender.Font.Color := clWindowText;
       end;

end;
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6152915
Yeah that was my understanding that the readonly would change at various times troughout the apps life.

Here is the same example from before but with some changes,

procedure TForm1.Button1Click(Sender: TObject);
begin

     DBEdit1.ReadOnly := not DBEdit1.ReadOnly;    
      SetDBEditColor(DBEdit1);    
 
end;

procedure TForm1.SetDBEditColor(Sender: TDBEdit);
begin

     If Sender.ReadOnly then begin
          Sender.Color := clBtnFace;
          Sender.Font.Color := clBtnText;
     end
     else begin
          Sender.Color := clWindow;
          Sender.Font.Color := clWindowText;
       end;

end;
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6152918
Sorry for the double post my finger flitched, really it did. :>)
0
 
LVL 3

Accepted Solution

by:
VSF earned 50 total points
ID: 6157316
  Most data aware components are capable of visually showing that they are disabled (by
                         changing the text color to gray) or enabled (by setting the color to a user-defined windows
                         text color).  Some data aware controls such as TDBGrid, TDBRichEdit (in Delphi 3.0) and
                         also TDBEdit (when connected to a numeric or date field) do not display this behavior.
                       Answer:

                           The code below uses RTTI (Run Time Type Information) to extract
                         property information and use that information to set the font color to
                         gray if the control is disabled. If the control is enabled, the text
                         color is set to the standard windows text color.

                           What follows is the step by step creation of a simple example which
                         consists of a TForm with a TButton and a TDBRichEdit that
                         demonstrates this behavior.

                           1.  Select File|New Application from the Delphi menu bar.
                           2.  Drop a  TDataSource, a TTable, a TButton and a TDBEdit
                               onto the form.
                           3.  Set the DatabaseName property of the table to 'DBDEMOS'.
                           4.  Set the TableName property of the table to 'ORDERS.DB'.
                           5.  Set the DataSet property of the datasource to 'Table1'.
                           6.  Set the DataSource property of the DBEdit to 'DataSource1'.
                           7.  Set the DataField property of the DBEdit to 'CustNo'.
                           8.  Set the Active property of the DBEdit to 'False'.
                           9.  Add 'TypInfo' to the uses clause of the form.

                         Below is the actual procedure to put in the implementation
                         section of your unit:

                         // This procedure will either set the text color of a
                         // dataware control to gray or the user defined color
                         // constant in clInfoText.

                         procedure SetDBControlColor(aControl: TControl);
                         var FontPropInfo: PPropInfo;
                         begin
                            // Check to see if the control is a dataware control
                            if (GetPropInfo(aControl.ClassInfo, 'DataSource') = nil) then
                              exit
                            else
                              begin
                                // Extract the front property
                                FontPropInfo:= GetPropInfo(aControl.ClassInfo, 'Font');
                                // Check if the control is enabled/disabled
                                if (aControl.Enabled = false) then
                                // If disabled, set the font color to gray
                                    TFont(GetOrdProp(aControl, FontPropInfo)).Color:=
                                    clGrayText
                                else
                                // If enabled, set the font color to clInfoText
                                  TFont(GetOrdProp(aControl, FontPropInfo)).Color:=
                                  clInfoText;
                               end;
                         end;

                         The code for the buttonclick event handler should contain:

                         //  This code will cycle through the Controls array and call
                         //  SetDbControlColor for each control on your form
                         //  making sure the font text color is set to what it
                         //  should be.

                         procedure TForm1.Button1Click(Sender: TObject);
                         var
                           i: integer;
                         begin
                         // Loop through the control array
                           for i:= 0 to ControlCount-1 do
                               SetDBControlColor(Controls[i]);
                         end;
0
 
LVL 3

Author Comment

by:siki
ID: 6159340
NO, PANDIAN! Your solution dosn't working. VSF posted good comment. It's about DBEdits, not simple TEdits. I am littke bit souprised, how hard is to change simple disabled edit color. I think, VB have done this in properties of comp.
0
 
LVL 3

Author Comment

by:siki
ID: 6159341
NO, PANDIAN! Your solution dosn't working. VSF posted good comment. It's about DBEdits, not simple TEdits. I am littke bit souprised, how hard is to change simple disabled edit color. I think, VB have done this in properties of comp.
0
 
LVL 3

Author Comment

by:siki
ID: 6159342
NO, PANDIAN! Your solution dosn't working. VSF posted good comment. It's about DBEdits, not simple TEdits. I am littke bit souprised, how hard is to change simple disabled edit color. I think, VB have done this in properties of comp.
0
 
LVL 3

Author Comment

by:siki
ID: 6159347
NO, PANDIAN! Your solution dosn't working. VSF posted good comment. It's about DBEdits, not simple TEdits. I am littke bit souprised, how hard is to change simple disabled edit color. I think, VB have done this in properties of comp.
0
 
LVL 3

Expert Comment

by:VSF
ID: 6163446
I'm glad I could help!

and thanks for the points.


VSF
www.enge.cjb.net
www.victory.hpg.com.br
UIN:14016999
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 15 hours left to enroll

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question