Forum > General
Check if at least 1 radiobutton is checked in radiogroup
dejong1968:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---VARi, NumOfCheckedItems: integer; RadioBut: string; procedure TForm1.SENDClick(Sender: TObject);begin NumOfCheckedItems := 0; for i := 1 to 3 do begin RadioBut:= 'RadioButton'+IntToStr(i); if (RadioBut.Checked = true) then NumOfCheckedItems += 1; end; if (NumOfCheckedItems = 0) then begin ShowMessage('Please check at least 1 item.'); end; end;
The codeline if (RadioBut.Checked = true) then ... gives an error: illegal qualifier.
How do I solve this?
Thanks in advance!
MarkMLl:
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
dseligo:
--- Quote from: dejong1968 on May 13, 2021, 10:31:20 am ---The codeline if (RadioBut.Checked = true) then ... gives an error: illegal qualifier.
--- End quote ---
You declared RadioBut as string - strings doesn't have Checked property.
lucamar:
--- Quote from: dejong1968 on May 13, 2021, 10:31:20 am ---I have a radiogroup with 3 radiobuttons. I want to check if at least one of them is checked.
--- End quote ---
Easiest way?:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if MyRadioGroup.ItemIndex < 0 then {No radio button checked};
ETA: As Mark already said. Sorry, didn't see the other answers :-[
dseligo:
And if you want to do it your way, this will work:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var NumOfCheckedItems: integer; procedure TForm1.Button4Click(Sender: TObject);var i:integer; rb: TComponent;begin NumOfCheckedItems := 0; for i := 1 to 3 do begin rb:=FindComponent('RadioButton'+IntToStr(i)); If (rb<>nil) and (rb is TRadioButton) and (rb as TRadiobutton).Checked then Inc(NumOfCheckedItems); end; if (NumOfCheckedItems = 0) then begin ShowMessage('Please check at least 1 item.'); end;end;
Navigation
[0] Message Index
[#] Next page