2
votes

I want read-only check boxes to be greyed out, but display their checked/unchecked status under Windows (XP and above), but I'm having some issues.

NOTE - Regarding 'read-only': It appears that Delphi's TCheckBox, doesn't even have a read-only option, this has been 'faked' by placing it on a TPanel and disabling that... However the question still remains valid, how does one achieve a read-only check-box that is greyed out, OR a inactive check-box that displays it's state.

Disabled checkboxes are greyed out, but these don't display a checked or unchecked state. Read-only check boxes can, but when Windows themes them, they just look like normal editable check boxes. The read-only box cannot have its value changed, but it looks like it can.

In XP with themes turned off (i.e. under classic mode), it works correctly.

Solutions that aren't acceptable, due to how clumsy/unprofessional they are for a large app or the development time/cash ratio of it, include these: - Manually greying the text and displaying an image of the checkbox status. - Disabling themes on the check-boxes, as the look without them is ugly. - Using custom checkboxes

Screenshots of the issue - These are three checked check boxes, one disabled, one read only and one normal:

enter image description here enter image description here

Although the read-only and editable check boxes appear different, that's just because the editable box in the first image has the focus. The read-only one will look the same if it's the one with the focus, as see in the second image.

2
If by inactive you mean disabled, I cannot duplicate your issue on XP, disabled check boxes show their check states. And I cannot find a read-only property of the control..Sertac Akyuz
Sorry, yes you're correct. By active and inactive I mean enabled and disabled.Anonymous
Ok. Better delete the question before you have a downvote. ;)Sertac Akyuz
I don't understand why this isn't a valid question @David. It asks how to get a disabled check box that still displays its checked state. Although there was misunderstanding about how to achieve a read-only check box, that's not actually important to the question.Rob Kennedy
@Rob I've no idea where those screenshots come from, but I've been using disabled vanilla check boxes, with checked state displayed, all the way back to Delphi 1!David Heffernan

2 Answers

1
votes

Checkboxes with themes show the checked mark when disabled, as you can see in this screenshot:

enter image description here

The dfm used to create this looks like this:

object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 337
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object CheckBox1: TCheckBox
    Left = 8
    Top = 8
    Width = 153
    Height = 17
    Caption = 'Disabled an checked'
    Checked = True
    Enabled = False
    State = cbChecked
    TabOrder = 0
  end
  object CheckBox2: TCheckBox
    Left = 8
    Top = 31
    Width = 153
    Height = 17
    Caption = 'Enabled and checked'
    Checked = True
    State = cbChecked
    TabOrder = 1
  end
  object CheckBox3: TCheckBox
    Left = 8
    Top = 54
    Width = 153
    Height = 17
    Caption = 'Disabled an un-checked'
    Enabled = False
    TabOrder = 2
  end
  object CheckBox4: TCheckBox
    Left = 8
    Top = 77
    Width = 153
    Height = 17
    Caption = 'Enabled and un-checked'
    TabOrder = 3
  end
end
0
votes

Anonymous has asked for code that demonstrates disabled check boxes showing their checked state.

program Project28;

uses
  Forms, StdCtrls;

var
  Form: TForm;

procedure Initialise;
var
  cb1, cb2: TCheckBox;
begin
  cb1 := TCheckBox.Create(Form);
  cb2 := TCheckBox.Create(Form);
  cb1.Parent := Form;
  cb2.Parent := Form;
  cb1.Top := 0;
  cb2.Top := 16;
  cb1.Enabled := False;
  cb2.Enabled := False;
  cb1.Checked := False;
  cb2.Checked := True;
  cb1.Caption := 'Checkbox1';
  cb2.Caption := 'Checkbox2';
end;

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm, Form);
  Initialise;
  Application.Run;
end.

enter image description here