Change Radio Group Without Firing OnClick

I have a radio group that has an onClick event that works correctly. However, when I change the itemIndex value through code, the onClick event fires too. How can I suppress the unwanted onClick (which in my mind should actually be an "onChange" event) event?

Thanks for any ideas.
LVL 1
surfboredAsked:
Who is Participating?
 
Ephraim WangoyaConnect With a Mentor Commented:

Thats how the Radio group is designed, to overcome this, do the following code

  RadioGroup1.OnClick = nil;   //remove the event before changing index
  try
     RadioGroup1.ItemIndex := 1;  //your value
  finally
     RadioGroup1.OnClick := RadioGroup1Click;  //restore the event
  end;
0
 
Ephraim WangoyaCommented:

There are other more complex methods you could use as well. Adding a variable to indicate when you are manually changing the ItemIndex

  eg
   private
      FIndexChanging: Integer;
 
   protected
      property IndexChanging: Boolean read GetIndexChanging write SetindexChanging;
   end

function TForm1.GetIndexChanging: boolen
begin
  Result := FIndexChanging>0;
end;

procedure TForm1.SetIndexChanging(value: boolean);
begin
  if Value then
    Inc(FIndexChanging)
  else if FIndexChanging >0 then
    Dec(FIndexChanging) ;
end;


TForm1.RadioGroup1Click(Sender: TObject);
begin
  if IndexChanging then
   Exit;

  ..........continue with your code
end;

//when changing index

  IndexChanging := True;
  try
     RadioGroup1.ItemIndex := 1;  //your value
    ................
  finally
     IndexChanging := False;
  end;
0
 
surfboredAuthor Commented:
Quick and easy, that's just the kind of answers I like!!! Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.