Mirror

How to set the item index in a TRadioGroup without firing the OnClick event (Views: 707)


Problem/Question/Abstract:

How to set the item index in a TRadioGroup without firing the OnClick event

Answer:

procedure SetRadioItem(radiogroup: TRadioGroup; index: Integer);
var
  ev: TNotifyEvent;
begin
  ev := radiogroup.OnClick;
  radiogroup.OnClick := nil;
  radiogroup.ItemIndex := index;
  radiogroup.Onclick := ev;
end;

A bit roundabout but it works. A checkbox could be treated similarly but I think you can also set its state by sending a BM_SETCHECK to it without having the OnClick event fire. This is untested:

procedure SetCheckbox(checkbox: TCheckbox; checked: Boolean);
const
  flags: array[boolean] of Integer = (BST_UNCHECKED, BST_CHECKED);
begin
  checkbox.Perform(BM_SETCHECK, flags[checked], 0);
end;

<< Back to main page