Recent

Author Topic: RadioGroup SelectionChanged  (Read 6304 times)

SolarPop

  • New Member
  • *
  • Posts: 10
RadioGroup SelectionChanged
« on: May 27, 2017, 02:58:00 am »
Lazarus Version #: 1.6.4

Someone please help me understand how to use the RadioGroup SelectionChanged and OnClick events. Both point to the following code. Neither cause it to execute. (Having the three Buttons OnClick events point to this same code will cause it to execute.)

procedure TForm1.RadioGroup1SelectionChanged(Sender: TObject);
begin
  ShowMessage('Radio group selection changed.');
  If RadioButton1.Checked = True Then
  ShowMessage('Button 1 selected');
  If RadioButton2.Checked = True Then
  ShowMessage('Button 2 selected');
  If RadioButton2.Checked = True Then
  ShowMessage('Button 3 selected');
end;
« Last Edit: May 27, 2017, 03:25:32 am by SolarPop »

sky_khan

  • Guest
Re: RadioGroup SelectionChanged
« Reply #1 on: May 27, 2017, 03:44:02 am »
You need to use it's Items property to create items, not seperate radiobuttons.
then you can use this:
Code: Pascal  [Select][+][-]
  1.  ShowMessage(Format('Radio group selection changed.'#13#10'Item %d is selected now.',[RadioGroup1.ItemIndex]));
  2.  

SolarPop

  • New Member
  • *
  • Posts: 10
Re: RadioGroup SelectionChanged
« Reply #2 on: May 27, 2017, 04:01:33 am »
Thanks SkyKhan, but I think I did not ask question clearly enough.

I created a form and placed a RadioGroup in the form. I then placed three RadioButtons inside RadioGroup.

When any change is made to the RadioGroup (SelectionChange or OnClick) I need to do something...say display a message, and then depending on which RadioButton was selected do something additional.

The SelectionChanged and OnClick events for the RadioGroup seem to not work. Nothing happens.

NOTE: The ShowMessage() lines in the first code I posted were for testing purposed only.

procedure TForm1.RadioGroup1SelectionChanged(Sender: TObject);
begin
  DoSomething0;
  If RadioButton1.Checked = True Then DoSomething1;
  If RadioButton2.Checked = True Then DoSomething2;
  If RadioButton2.Checked = True Then DoSomething3;
end;

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: RadioGroup SelectionChanged
« Reply #3 on: May 27, 2017, 04:14:54 am »
@solarpop:

You are mixing two different solutions.
a. 1 panel, 3 radiobuttons, point events to same (radiobutton) event handler
b. 1 radiogroup, 3 items belonging to that radiogroup (property items), using a single event handler of the radiogroup.

If you wish to knw more about TRadioGroup, then see this wiki page, then decide which solution you want to use.

SolarPop

  • New Member
  • *
  • Posts: 10
Re: RadioGroup SelectionChanged
« Reply #4 on: May 27, 2017, 05:12:55 am »
Thanks to both of you! RadioGroup SelectionChanged does work!

I wasn't knowledgeable enough initially to understand SkyKhan's response, but now I understand what I was being told was the solution. I guess, "it takes a village of programmers to train a novice like me."

RadioGroup2.ItemIndex := 0;   {in ShowForm to initialize with first item selected}

procedure TForm1.RadioGroup2SelectionChanged(Sender: TObject);
  begin
  ShowMessage('RadioGroup2.Index: '+ IntToStr(RadioGroup2.ItemIndex));
  If RadioGroup2.ItemIndex = 0 Then ShowMessage('First item selected');
  If RadioGroup2.ItemIndex = 1 Then ShowMessage('Second item selected');
  If RadioGroup2.ItemIndex = 2 Then ShowMessage('Third item selected');
  end;


Thanks again!

 

TinyPortal © 2005-2018