Topic: 4 Checkboxes, have only 1 selected

Hello, i have 4 checkboxes and I only want to be able to select 1 of the 4.  So if i select checkbox 1 then I select checkbox 2, checkbox 1 will automatically deselect.

here is the project attached. i wrote a seperate 'Onclick' for each checkbox  but i cant get it to work, any ideas?

\\sample procedure
procedure Two_form2_CheckBox2 (Sender: TObject);
begin
Sleep(300);
form2.Checkbox1.State := cbUnchecked;
form2.CheckBox3.State := cbUnchecked;
form2.CheckBox4.State := cbUnchecked;
end;
Post's attachments

Attachment icon Checkbox_myvdb.zip 324.92 kb, 195 downloads since 2022-03-20 

Re: 4 Checkboxes, have only 1 selected

Hello mrgoodman99

And if you try this

form2.Checkbox1.Checked := not form2.Checkbox1.Checked;
form2.CheckBox2.Checked := not form2.Checkbox2.Checked;
form2.CheckBox3.Checked := not form2.Checkbox3.Checked;
form2.CheckBox4.Checked := not form2.Checkbox4.Checked;

JB

Re: 4 Checkboxes, have only 1 selected

procedure checkBoxTest (Sender: TObject);
var
i:integer;
begin
  for i:=0 to Form2.ComponentCount-1 do
    if TdbCheckBox(Form2.Components[i] is TdbCheckBox) then
       if (TdbCheckBox(Form2.Components[i]).Name <> TdbCheckBox(Sender).Name) then TdbCheckBox(Form2.Components[i]).Checked := False;
  TdbCheckBox(Sender).Checked := True;
end;
Post's attachments

Attachment icon Checkbox.rar 2.45 kb, 214 downloads since 2022-03-20 

Re: 4 Checkboxes, have only 1 selected

procedure checkBoxTest (Sender: TObject);

thank you for the reply, it works great.

Re: 4 Checkboxes, have only 1 selected

quick question regarding the procedure

i see Sendeer alot within the script...  TdbCheckBox(Sender)

how do i interpret 'Sender'.  thnaks

Re: 4 Checkboxes, have only 1 selected

Hello mgoodman99

Sender is a reference to the component that fired the event. In this case, Sender is going to be the button the user clicked which called your Button1Click event.
This is useful when you have several components that call the same event and you need to figure out which component caused the event to be fired.

JB

Re: 4 Checkboxes, have only 1 selected

thanks, it makes sense now...

wish there was a way to give a thumbs up or mark as solved. no big deal thought

Re: 4 Checkboxes, have only 1 selected

procedure checkBoxTest (Sender: TObject);

regarding the proceduer you provided... it works as expected but need to add another functionality...

i have the Submit button below the checkboxes.  The submit button saves the choice to the database AND i also want to reset the checkboxes to Unchecked...  i tried several ways but it seems the Checked Box cannot be UnChecked (at least I cant)

i tried the same looping Components as an OnClick for the submit button but cannot get it to work. 

Given this procedure you provided.  If I press the Submit button how can I reset all the CheckBoxes to Unchecked?

for i:=0 to Form2.ComponentCount-1 do
      if TdbCheckBox(Form2.Components[i] is TdbCheckBox) then
              if tdbCheckbox(form2.components[i]).State = cbChecked then
                  tdbCheckbox(form2.components[i]).State = cbUnchecked;

attached is the program.  thanks for the help

Post's attachments

Attachment icon Checkbox_forumResponse.zip 326.05 kb, 193 downloads since 2022-03-24 

Re: 4 Checkboxes, have only 1 selected

for i:=0 to Form2.ComponentCount-1 do
      if TdbCheckBox(Form2.Components[i] is TdbCheckBox) then
         TdbCheckBox(Form2.Components[i]).Checked := False;

Re: 4 Checkboxes, have only 1 selected

Hi MGoodman99, Hi Sparrow,
Just had a quick look at this and as far as I can tell, the problem is whenever you try to uncheck your checkboxes, it causes the original procedure  'checkboxtest' to fire off again (the old 'deadly embrace' - LOL!).
The easiest fix is probably just to use a variable to break the cycle (please see attached).
Derek.

Post's attachments

Attachment icon Checkbox_forumResponse 2.zip 336.51 kb, 220 downloads since 2022-03-24 

11 (edited by sparrow 2022-03-24 19:39:17)

Re: 4 Checkboxes, have only 1 selected

Hi Derek


Yes, I did not take into account this moment  LOL!).
Thanks


or use if NOT Form2.Button1.Focused then ...

in procedure checkBoxTest

Re: 4 Checkboxes, have only 1 selected

Hi Sparrow,
'Testing for form2.button1.focused' is more elegant.
Thanks for the suggestion.
Derek.

Re: 4 Checkboxes, have only 1 selected

both methods work.

thanks for the quick replies