DBEdit not enabled font black not greyed out

If I use a DBEdit set to not enabled then the font is greyed out, is there anyway to set the font to black as I want it to standout.

Also can a DBEdit alignment be set to := taCenter

regards
Steve

Comments

  • steve53 wrote:
    If I use a DBEdit set to not enabled then the font is greyed out, is there anyway to set the font to black as I want it to standout.
    Not easily, no. The use of clGrayText when the control is disabled is hard-coded in the TDBEdit control. I think the only way would be to create a new component, basically copying the TDBEdit code, but allowing the font to be a different color when the control is not enabled. That is a lot of work, and creates a maintenance issue as you may need to repeat the process in later Delphi versions if TDBEdit changes for any reason.
    I always like a lazy solution and I would be inclined to fake it instead, like this:
    Add a private member to your form:
    TForm1 = class(TForm)
    ...
    private
      FDBEdit1_Enabled : boolean;
    
    Set the initial value of this variable in your form's onCreate event:
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      FDBEdit1_Enabled := TRUE;  // Or FALSE, your choice
    
    Then instead of changing the Enabled property of the DBEdit (DBEdit1.Enabled := FALSE) you set the value of your new member var:
    FDBEdit1_Enabled := FALSE;
    
    Now the "magic" is that you add an onKeyPress event handler to your DBEdit control and filter out all key presses if your new member var is FALSE.
    procedure TForm1.DBEdit1KeyPress(Sender: TObject; var Key: Char);
    begin
      assert(DBEdit1.Enabled,'Use FDBEdit1_Enabled !!');
      if not FDBEdit1_Enabled then
        Key := char(0);
    end;
    
    Now your control will be enabled all the time, but it simply will not accept any keystrokes when your member var forbids it. For most purposes this will be good enough.
    The assert is there in case you ever forget about your fakery and revert to the standard way of disabling the DBEdit.
    Also can a DBEdit alignment be set to := taCenter
    Again, no, not easily. The DBEdit control alignment is set by the type of database field which supplies it with data. So again, you are faced with creating your own control or faking it. To fake it you could change the DBEdit control to a DBMemo control because the DBMemo control has an Alignment property. Set its size to be the same as the DBEdit and it would work. The main problem is that the DBMemo would accept multiple lines which you don't want. Microsoft's Windows documentation suggest you could send the DBMemo a EM_SETEDITSTYLEEX message with SES_EX_USESINGLELINE but I have not been able to find a value for SES_EX_USESINGLELINE. If you can find that value, please let me know! Otherwise I guess you would have to add code to strip out carriage returns. Don't forget to handle text pasted in from the clipboard, and strip out text formatting. Add handlers for the DBMemo's onChange and onKeyPress events:
    procedure TForm1.DBMemo1Change(Sender: TObject);
    var
      CRPos : integer;
    begin
      CRPos := Pos(#13, DBMemo1.Text);
      if CRPos > 0 then
        DBMemo1.Text := Copy(DBMemo1.Text, 1, CRPos-1)
    end;
    
    procedure TForm1.DBMemo1KeyPress(Sender: TObject; var Key: Char);
    begin
      if CharInSet(Key,[#10, #13]) then
        Key := #0
    end;
    
    I think that should do it.
    NB: CharInSet is not present in older Delphi versions so if the compiler barfs on that change the line to
    if Key in [#10, #13] then
    
  • Hi SalmiSoft, many thanks for your reply, sorry I haven't replied sooner, I have been away for a few days. I will give the info you have supplied a try and see how it goes, it does look like it may be a long winded job and I will probably loose a lot of sleep over it but hey, that's what it's all about isn't it lol.

    regards
    Steve
  • So how did you get on? Do you have this working now? It would be interesting to know which approach you found was the easiest - or if you decided it was more effort than it was worth!
  • Yes you guested it, I went to for the easy option, I sent all the DBEdit's to;

    Enable=true and ReadOnly=True

    Edit is disabled but they are now black and not greyed out, with the exception of the 2 TDBComboBox's I have, but hey I can live with that.

    regards
    Steve

    P.S. Many thanks for all your help though, very much appreciated.
Sign In or Register to comment.