Setting TRadioButton to checked causes OnClick event
mybox.Checked := true;
开发者_高级运维
Setting TRadioButton to checked (by code) causes OnClick event handler to be called.
How can I recognize if user is making the state change by GUI interaction
You can nil the OnClick event handler while changing a radiobutton state programmatically:
procedure TForm1.Button6Click(Sender: TObject);
var
Save: TNotifyEvent;
begin
Save:= RadioButton2.OnClick;
RadioButton2.OnClick:= nil;
RadioButton2.Checked:= not RadioButton2.Checked;
RadioButton2.OnClick:= Save;
end;
mybox.Tag := 666;
mybox.Checked :=true;
mybox.Tag := 0;
procedure myboxOnclick(Sender : TObject);
begin
if Tag = 0 then
//Do your thing
end;
If you have an action connected to the radiobutton, you can set the checked property of the action instead. This will also prevent the OnClick event to be fired.
TRadioButton
(like TCheckBox
) provides a protected property ClicksDisabled
that can help you.
I use class helpers to add the needed functionality:
RadioButton1.SetCheckedWithoutClick(False);
with the following class helper for a VCL TRadioButton
:
TRadioButtonHelper = class helper for TRadioButton
procedure SetCheckedWithoutClick(AChecked: Boolean);
end;
procedure TRadioButtonHelper.SetCheckedWithoutClick(AChecked: Boolean);
begin
ClicksDisabled := True;
try
Checked := AChecked;
finally
ClicksDisabled := False;
end;
end;
精彩评论