Recent

Author Topic: Check if at least 1 radiobutton is checked in radiogroup  (Read 3163 times)

dejong1968

  • Newbie
  • Posts: 2
Check if at least 1 radiobutton is checked in radiogroup
« on: May 13, 2021, 10:31:20 am »
Hi,

I have a radiogroup with 3 radiobuttons. I want to check if at least one of them is checked. If not: errormessage. I've tried this:

Code: Pascal  [Select][+][-]
  1. VAR
  2. i, NumOfCheckedItems: integer;
  3.   RadioBut: string;
  4.  
  5. procedure TForm1.SENDClick(Sender: TObject);
  6. begin
  7.   NumOfCheckedItems := 0;
  8.   for i := 1 to 3 do
  9.   begin
  10.     RadioBut:= 'RadioButton'+IntToStr(i);
  11.     if (RadioBut.Checked = true) then
  12.     NumOfCheckedItems += 1;
  13.   end;
  14.   if (NumOfCheckedItems = 0) then
  15.     begin
  16.          ShowMessage('Please check at least 1 item.');
  17.     end;
  18.  
  19.  
  20. end;

The codeline if (RadioBut.Checked = true) then     ... gives an error: illegal qualifier.

How do I solve this?

Thanks in advance!




MarkMLl

  • Hero Member
  • *****
  • Posts: 2689
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #1 on: May 13, 2021, 10:47:07 am »
I'd have thought that this wouldn't be necessary, since the intention of a radiogroup is to have precisely one button pressed. However I think that you could usefully check that ItemIndex is >=0 and in the expected range.

(Antique radios had "piano keys" https://www.radiomuseum.org/r/pye_vhf2dvhf_2.html with a mechanical interlock ensuring that only one could be pressed down at a time, hence "radio button".)

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

dseligo

  • Sr. Member
  • ****
  • Posts: 275
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #2 on: May 13, 2021, 10:50:52 am »
The codeline if (RadioBut.Checked = true) then     ... gives an error: illegal qualifier.
You declared RadioBut as string - strings doesn't have Checked property.

lucamar

  • Hero Member
  • *****
  • Posts: 4006
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #3 on: May 13, 2021, 10:54:29 am »
I have a radiogroup with 3 radiobuttons. I want to check if at least one of them is checked.

Easiest way?:

Code: Pascal  [Select][+][-]
  1. if MyRadioGroup.ItemIndex < 0 then
  2.   {No radio button checked};

ETA: As Mark already said. Sorry, didn't see the other answers :-[
« Last Edit: May 13, 2021, 10:56:59 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

dseligo

  • Sr. Member
  • ****
  • Posts: 275
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #4 on: May 13, 2021, 10:59:23 am »
And if you want to do it your way, this will work:

Code: Pascal  [Select][+][-]
  1. var NumOfCheckedItems: integer;
  2.  
  3. procedure TForm1.Button4Click(Sender: TObject);
  4. var i:integer;
  5.     rb: TComponent;
  6. begin
  7.   NumOfCheckedItems := 0;
  8.   for i := 1 to 3 do
  9.   begin
  10.     rb:=FindComponent('RadioButton'+IntToStr(i));
  11.     If (rb<>nil) and (rb is TRadioButton) and (rb as TRadiobutton).Checked then
  12.       Inc(NumOfCheckedItems);
  13.   end;
  14.   if (NumOfCheckedItems = 0) then
  15.   begin
  16.     ShowMessage('Please check at least 1 item.');
  17.   end;
  18. end;

MarkMLl

  • Hero Member
  • *****
  • Posts: 2689
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #5 on: May 13, 2021, 11:08:12 am »
ETA: As Mark already said. Sorry, didn't see the other answers :-[

Don't worry, I always appreciate a sanity check :-)

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

dejong1968

  • Newbie
  • Posts: 2
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #6 on: May 14, 2021, 08:52:49 am »
Thank you all for your answers.

The code from lucamar
Code: Pascal  [Select][+][-]
  1. if MyRadioGroup.ItemIndex < 0 then
didn't work. I stilll get the error when I select a radiobutton.

The code from dseligo worked. I looked the syntax up and understand what is means/does. 
Problem solved, thanks to you all.


MarkMLl

  • Hero Member
  • *****
  • Posts: 2689
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #7 on: May 14, 2021, 09:20:09 am »
The code from lucamar
Code: Pascal  [Select][+][-]
  1. if MyRadioGroup.ItemIndex < 0 then
didn't work. I stilll get the error when I select a radiobutton.

Are you setting ItemIndex at design time? I think you'd normally do this to set up the default state of the group.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Handoko

  • Hero Member
  • *****
  • Posts: 4285
  • My goal: build my own game engine using Lazarus
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #8 on: May 14, 2021, 09:31:40 am »
Lucamar's code: RadioGroup1.ItemIndex < 0 should not generate any error. It works on my test:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   if RadioGroup1.ItemIndex < 0 then
  4.     ShowMessage('You have to select an item.');
  5. end;

wp

  • Hero Member
  • *****
  • Posts: 8347
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #9 on: May 14, 2021, 09:37:49 am »
Do you really mean a TRadioGroup? For this Lucamar's code MUST work. Or do you mean a TGroupbox with individual radiobuttons? Although it may look the same it is organized differently internally, in particular there will be not ItemIndex, which could explain why Lucamar's code is not working for you.
« Last Edit: May 14, 2021, 09:45:28 am by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

dseligo

  • Sr. Member
  • ****
  • Posts: 275
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #10 on: May 14, 2021, 09:40:57 am »
Lucamar's code: RadioGroup1.ItemIndex < 0 should not generate any error. It works on my test:

From code the OP posted it was obvious that he doesn't have control 'TRadioGroup', but individual 'TRadioButton'.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2689
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #11 on: May 14, 2021, 10:04:13 am »
From code the OP posted it was obvious that he doesn't have control 'TRadioGroup', but individual 'TRadioButton'.

But OP's question specifically said "radiogroup", and he was doing something very odd with a string. I think we could usefully see more of what he's trying to do, since there's a possibility that he's abusing the controls in a way that will give him problems later on.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Handoko

  • Hero Member
  • *****
  • Posts: 4285
  • My goal: build my own game engine using Lazarus
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #12 on: May 14, 2021, 10:21:29 am »
Yes, that explains why I didn't get the error.

dseligo

  • Sr. Member
  • ****
  • Posts: 275
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #13 on: May 14, 2021, 10:27:01 am »
From code the OP posted it was obvious that he doesn't have control 'TRadioGroup', but individual 'TRadioButton'.

But OP's question specifically said "radiogroup", and he was doing something very odd with a string. I think we could usefully see more of what he's trying to do, since there's a possibility that he's abusing the controls in a way that will give him problems later on.

MarkMLl

Yes, he said radiogroup but from his code it was clear that he means 'group of radiobuttons'. He tried to access individual controls but he didn't use FindComponent.
I attached small example.

wp

  • Hero Member
  • *****
  • Posts: 8347
Re: Check if at least 1 radiobutton is checked in radiogroup
« Reply #14 on: May 14, 2021, 11:18:05 am »
Yes, he said radiogroup but from his code it was clear that he means 'group of radiobuttons'. He tried to access individual controls but he didn't use FindComponent.
I attached small example.
No, not clear at all. My example uses a TRadioGroup, accesses individual controls and does not use FindComponent. So, when somebody is accessing individual radiobuttons of a TRadioGroup does not necessarily mean that he is talking of a "TGroupbox" rather than a "TRadioGroup".
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

 

TinyPortal © 2005-2018