Lazarus

Programming => General => Topic started by: dejong1968 on May 13, 2021, 10:31:20 am

Title: Check if at least 1 radiobutton is checked in radiogroup
Post by: dejong1968 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!



Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: MarkMLl 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
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: dseligo 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.
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: lucamar 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 :-[
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: dseligo 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;
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: MarkMLl 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
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: dejong1968 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.

Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: MarkMLl 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
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: Handoko 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;
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: wp 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.
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: dseligo 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'.
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: MarkMLl 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
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: Handoko on May 14, 2021, 10:21:29 am
Yes, that explains why I didn't get the error.
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: dseligo 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.
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: wp 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".
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: Handoko on May 14, 2021, 11:22:41 am
Show us the whole source code, that is what I always ask. If OP provides the whole compile-able source code, the issue should be able to solve in minutes.
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: dseligo on May 14, 2021, 12:26:52 pm
Show us the whole source code, that is what I always ask. If OP provides the whole compile-able source code, the issue should be able to solve in minutes.

I posted my code 28 minutes after he asked and he said that it works for him.
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: dseligo on May 14, 2021, 12:41:03 pm
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".

I read his code as attempt to check if there is selected at least one radio button in multiple radio button groups. He tried to solve it by counting checked radio buttons, which he wanted to reference by name. I showed him how to do it and he said that it works for him.
Of course, there are other ways to solve this. In your example it isn't shown how to check if any radio button is selected out of multiple TRadioGroups. And what if he has radio buttons outside of TGroupbox, just on the form?
No hard feelings.
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: MarkMLl on May 14, 2021, 12:44:10 pm
Of course, there are other ways to solve this. In your example it isn't shown how to check if any radio button is selected out of multiple TRadioGroups. And what if he has radio buttons outside of TGroupbox, just on the form?
No hard feelings.

And that is why both Handoko and I have said that OP could usefully show us what he's trying to do, since abusing the functionality that the developers put into the standard controls never ends well.

MarkMLl
Title: Re: Check if at least 1 radiobutton is checked in radiogroup
Post by: Handoko on May 14, 2021, 12:48:11 pm
Show us the whole source code, that is what I always ask. If OP provides the whole compile-able source code, the issue should be able to solve in minutes.

I posted my code 28 minutes after he asked and he said that it works for him.

If OP provided the whole source code on his first post, I believe most or maybe all of us can solve it in less than 10 minutes. Compile-time errors are very easy to solve if the whole source code is provided. Not only solving the problem, sometimes there will be suggestions for improving the code quality.

Without the compile-able source code, we just guess. But if I had it, I simple open it using Lazarus, inspect and can fully understand the problem. Lazarus is an awesome IDE tool for debugging.

Usually posters only show some lines of the code where they believe are the cause of the issue. But often, the real problem is on somewhere else.
TinyPortal © 2005-2018