24 Votes

Delphi: Change RadioButton.Checked or CheckBox.Checked without triggering OnClick-Event

Tip by Delphian | 2013-03-21 at 20:00

When changing the state of a RadioButton or a Checkbox by code - for example when setting Checked to True or False - automatically, the OnClick-Event of the option switch will be triggered. Today, I want to show you, how you can prevent the OnClick Event from executing.

OnClick-Event will be executed

RadioButton1.Checked := True;

// or

CheckBox1.Checked := True;

OnClick-Event will be prevented

procedure ChangeState(NewState: boolean);
var
  OldEvent: TNotifyEvent;
begin
  // remember old event
  OldEvent := RadioButton1.OnClick;
  RadioButton1.OnClick := nil;

  // apply changes, for example:
  RadioButton1.Checked := NewState;

  // reset event
  RadioButton1.OnClick := OldEvent;
end;

The trick is: We remember the procedure assigned to the OnClick Event in the variable OldEvent. After that, we set OnClick to nil. With this, there is no procedure assigned anymore and we can apply the changes without any problems. Because, also after the changes, it should work like before, we reset the event in a last step. In the example, we have used a RadioButton, but, of course, this also works with a CheckBox in the same way.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

VirtualBox: Change Date and Time

Tutorial | 10 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.