Solved

Read Only Checkbox

Posted on 2004-04-21
9
288 Views
Last Modified: 2010-05-18
Hi all.

Hopefully this is an easy question..

I am writing an application that displays the status of true false attributes of an account using check boxes on a form.

I do not want the users to be able to uncheck - re-check the check boxes, but do not want the checkboxes to appear disabled as they tend to blend in with the form and look crappy.. (Technical term).

Is there a way to make the check boxes read only, or is there another check box component I can use for this??
0
Comment
Question by:BedouinDN
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 125 total points
ID: 10883516
Will need to derive a new component and handle the Mouse events yoruself:

type
  TReadOnlyCheckBox = class(TCheckBox)
  private
    fReadOnly: Boolean;
  protected
    procedure WmLButtonDown(var Msg: TWMLButtonDown);
        message Wm_LButtonDown;
    procedure WmLButtonUp(var Msg: TWMLButtonUp);
        message Wm_LButtonUp;
    procedure WmLButtonDblClk(var Msg: TWMLButtonDblClk);
        message Wm_LButtonDblClk;
  published
    property ReadOnly: Boolean read fReadOnly write fReadOnly;
  end;

...

procedure TReadOnlyCheckBox.WmLButtonDown(var Msg: TWMLButtonDown);
begin
  if not ReadOnly then
    inherited
  else
    Msg.Result := 0;
end;

procedure TReadOnlyCheckBox.WmLButtonUp(var Msg: TWMLButtonUp);
begin
  if not ReadOnly then
    inherited
  else
    Msg.Result := 0;
end;

procedure TReadOnlyCheckBox.WmLButtonDblClk(var Msg:
TWMLButtonDblClk);
begin
  if not ReadOnly then
    inherited
  else
    Msg.Result := 0;
end;



Shane
0
 

Expert Comment

by:steveaish
ID: 10883524
Not the tidiest way but have a private variable called Check1Checked type boolean

type
  TForm1 = class(TForm)
    CheckBox1: TCheckBox;
    procedure FormCreate(Sender: TObject);
  private
    Check1Checked : Boolean;

when you make your checkbox checked or unchecked also set this variable (Check1Checked := Checkbox1.Checked)

in the OnClick event of the Checkbox

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  if Checkbox1.Checked <> Check1Checked then
  begin
    Checkbox1.Checked := Check1Checked;
  end;
end;

0
 
LVL 17

Expert Comment

by:mokule
ID: 10883616

My solution to every problem is TPanel :)

Place CheckBoxes on a TPanel and set

Panel1.Enabled := False;

That's all
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 4

Author Comment

by:BedouinDN
ID: 10883696
Have accepted Shanes answer as I will be using this a lot and so will create a new derived Checkbox, Mokule - Your answer works beautifully and I will use yours for now until I get the time to create. Please look for "Points for Mokule" in the list for the same amount of points..

Steveaish, I have tried to do this but ended up in an endless loop until stack overflow - Seems setting the checked value calls the OnCLick method.. :-(

Thanks again guys..

Colin.
0
 
LVL 17

Expert Comment

by:mokule
ID: 10883698
Why You don't like TPanel :(
0
 
LVL 4

Author Comment

by:BedouinDN
ID: 10883703
Beat ya..
:-)
0
 
LVL 17

Expert Comment

by:mokule
ID: 10883711
Ooops. And yet You like it also. Thanks :))
I was too fast.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10883807
I like Mokule solution as well, i may use that in the future for speediency.... <SMILE>

Shane
0
 
LVL 17

Expert Comment

by:mokule
ID: 10883904
Wow :)
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

776 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