tags:

views:

100

answers:

3
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

+4  A: 
 mybox.Tag := 666; 
 mybox.Checked :=true; 
 mybox.Tag := 0;

procedure myboxOnclick(Sender : TObject);
begin
if Tag = 0 then
//Do your thing
end;
Mihaela
I favor this approach but usually use an flag in the private section of the form class, something along the lines of "ChangingStuffRomCode:Boolean". The idea's that there usually are multiple radio buttons (and other controls that behave like this) and it's simpler to do set a single flag when initializing the form.
Cosmin Prund
+6  A: 

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;
Serg
Ideally you should wrap this in a try..finally if there was any more complex logic between `OnClick := nil` and `OnClick := Save;`
Gerry
+1  A: 

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.

Uwe Raabe
But if you're using actions, you're probably using the action's OnExecute event, not the control's OnClick event. Does OnExecute get fired when you change the action's Checked property?
Rob Kennedy
@Rob Kennedy: No, it does not - Action.OnExecute and Button.OnClick is the same event here. An action temporally sets the radiobutton's protected 'ClicksDisabled' property to True to prevent radiobutton's 'OnClick' event to fire while changing 'Checked' property.
Serg