Page 1 of 1

Question Involving CheckBoxes

Posted: Mon Jul 23, 2007 5:35 pm
by WaywornMmmmm
Well, in my attempt to complete this application more questions have arisen.

On my form I have placed three checkboxes. Is there a way to make only two of these three checkboxes checked at a time?


-Thank you.

Posted: Fri Aug 10, 2007 9:51 pm
by OwnZ joO
Do you need 2 pressed at all times? Or could you just have 1 pressed, and 2 at most?

And yes there is a way, I don't know if theres one by just changing properties of the checkboxes, but if not you can figure out a way to do it.

Posted: Fri Aug 10, 2007 10:48 pm
by StalkingGrunt911
So you mean if you check one the other checks too? If so just double click on the one you want to check to make the other checkbox checked off to.
The code if Visual Basic would be something like

Code: Select all

//CheckBox1 & 2 just being as the name for whatever checkbox you plan to use 
Public Sub CheckBox1_Checked(ByVal sender As Object, ByVal e As EventArgs) As Handles.CheckBox1_Checked
	If CheckBox1.Checked = True Then
		Me.CheckBox2.Checked = True
	End If
End Function
or if it's C# something like

Code: Select all

private sub checkBox1_Checked (object sender, EventArgs e)
{
  if(checkBox1.Checked == true)
  {
     this.checkBox2.Checked = true;
  }
}
I believe that is it, I haven't opened up VB or C# in a while but I'm pretty sure that's how to do it.

Posted: Sat Aug 11, 2007 3:05 am
by Prey
It is, but I think he meant that if two were checked, you could not check a third. If that is the case,

Code: Select all

        private void checkBox1_2_3_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox box = (sender as CheckBox);

            if (checkBox1.Checked && checkBox2.Checked && checkBox3.Checked)
                box.Checked = false;
        }
That method is linked to all the checkbox's CheckChanged event, but if you do need to separate it out for w/e reason then you can easily put it in it's own function and call it as needed.

Posted: Sat Aug 11, 2007 8:37 am
by OwnZ joO
Haha I was thinking more complicated than I needed to be, I was thinking of a way to make it so when you checked a check box, and there were 3 being checked, the first one that you had checked would become unchecked and you'd check the one that you just clicked.

Also prey what's with the (sender as CheckBox)? is that the same as casting it, or does it have different performance.

Posted: Sat Aug 11, 2007 8:53 am
by Prey
OwnZ joO wrote:Also prey what's with the (sender as CheckBox)? is that the same as casting it, or does it have different performance.
It is casting, just another way of doing it. This will also only work for reference types though, so I normally use it for such things only because of its readability; it flows better than doing it the other way, and thus goes further towards maintaining good code that is easily understandable, something you should always strive to do with any project.

Posted: Sat Aug 11, 2007 9:02 am
by OwnZ joO
Yeah, I do strive for that, I have seen that before, but I just never knew exactly what it did, if it was any different than casting. Good to know, thanks.