Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to make TCheckBox Readonly?

770 views
Skip to first unread message

Diviner

unread,
Nov 20, 2001, 4:56:26 AM11/20/01
to
I can't see Readonly property there. So how to workaround?

Thank you very much.
--
Best regards,
Diviner.

Paul Ashton

unread,
Nov 20, 2001, 6:05:34 AM11/20/01
to

"Diviner" <div...@icare.com.hk> schrieb im Newsbeitrag
news:3BFA28CA...@icare.com.hk...

> I can't see Readonly property there. So how to workaround?

The easiest way is to dissable it.

ie MyCheckBox.Enabled := false;

You can still use the Checked property, but the box is greyed.


Paul Ashton

unread,
Nov 22, 2001, 4:16:02 AM11/22/01
to

"Diviner" <div...@icare.com.hk> schrieb im Newsbeitrag
news:3BFC7135...@icare.com.hk...
> But it is not good for viewing and its look feeling is not
> consistence to other components if make it disable.

The problem I,ve always had with TCheckBox is that the OnClick event is
actually an OnChangeCheckState event.
Set the Checked property ie MyCheckBox.Checked := Boolean and OnClick still
comes. Resetting the state in OnClick causes and endless loop. I've always
found dissable sufficient and never bothered to find an alternative way of
doing it.

This has always been like this. I'm now using D4 but it was the same in D2.

What would be useful is an
OnBeforeStateChange(Sender : TObject ; State : Boolean, var CanDo :
Boolean);

Does anybody else know a way of doing what Deviner wants without deriving
from TCheckBox and overriding some method or other.

And is TCheckBox different in later Delphi's.

Raymond Kennington

unread,
Nov 22, 2001, 4:29:04 AM11/22/01
to
Have you tried SysUtils.Abort() in the OnChange (or OnClick) event?

If that doesn't work then you can manage the state yourself and use code in the OnChange event like

inherited;

if not UserCanChange then
begin
TCheckBox(Sender).Checked := not TCheckBox(Sender);
end;

That is, let the change be made but immediately change it back. The user shouldn't see this.

Raymond KEnnington

Arthuro

unread,
Nov 22, 2001, 5:28:46 AM11/22/01
to
"Paul Ashton" <paul....@admin-itc.de> wrote in message
news:3bfcc1a6_1@dnews...

I have a better way of doing this.
I have implemented a readonly property for the checkbox that works as
expected.
Only thing is i'm at work now, so you'll have to wait till this evening b4 i
can post it.
(That is if i can get my connection up today, else i'll have to make a
floppy and post it tomorrow)..

Greetz,
Arthuro..

Diviner

unread,
Nov 22, 2001, 11:54:59 PM11/22/01
to Arthuro
Hi, Arthuro,

Thank you very much. May I have a copy of your effort? :)

--
Best regards,
Diviner.

Arthuro

unread,
Nov 26, 2001, 7:22:45 AM11/26/01
to

"Diviner" <div...@icare.com.hk> wrote in message
news:3BFDD6A3...@icare.com.hk...

> Hi, Arthuro,
>
> Thank you very much. May I have a copy of your effort? :)
>
As soon as i get to a working internet conenction i'll post the source code.
It's quite simple actually.
It's not the neatest of solutions but it works..

Arthuro

unread,
Nov 27, 2001, 5:37:25 PM11/27/01
to

"Arthuro" <hexor...@vaxor.com> schreef in bericht
news:3c0246cd$1_1@dnews...

As i have promised here it is, my solution for the readonly checkbox.

What i do is overriding the toggle event method which is launched at the
precise moment that you click the checkbox to change it. I do an adittional
check against the new readonly property, and if its set i ignore the toggle
command, so the change never happens.
It's a crude hack but utterly simple, and verry clean..
The increase in size for the checkbox component is next to nothing...


Type
TReadOnlyCheckBox = Class(TCheckBox)
Private
{ Private declarations }
FReadOnly: Boolean;
Protected
{ Protected declarations }
Procedure Toggle; Override;
Public
{ Public declarations }
Constructor Create(AOwner: TComponent); override;
Published
{ Published declarations }
Property ReadOnly: Boolean Read FReadOnly Write FReadOnly;
End;

Implementation

Procedure TReadOnlyCheckBox.Toggle;
Begin
If Not FReadOnly Then
Inherited Toggle;
End;

Constructor TReadOnlyCheckBox.Create(AOwner: TComponent);
Begin
FReadOnly:= True;
Inherited Create(AOwner);
End;


Arthuro

unread,
Nov 27, 2001, 5:40:09 PM11/27/01
to
"Raymond Kennington" <raym...@chariot.net.au> schreef in bericht
news:3BFCC560...@chariot.net.au...

But he will, if he has a fast computer.
I think my solution is better, although it works along the same lines.
I make it so that the change never happens in the first place.
The checkbox is never redrawn, and the status never changes.. See my other
post for the details details..

0 new messages