Question Involving CheckBoxes

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
Post Reply
User avatar
WaywornMmmmm




Coroner

Posts: 1341
Joined: Sat Nov 05, 2005 5:17 am
Location: U.S.A

Question Involving CheckBoxes

Post 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.
OwnZ joO




Articulatist 500

Posts: 980
Joined: Thu Nov 10, 2005 4:24 pm

Post 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.
User avatar
StalkingGrunt911




Recreator Connoisseur Acolyte Coroner
Sigma Pyre

Posts: 3618
Joined: Wed May 24, 2006 12:30 pm
Location: Florida!
Contact:

Post 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.
User avatar
Prey




Connoisseur Snitch! Pyre Articulatist 500

Posts: 1026
Joined: Wed Dec 27, 2006 6:49 am
Location: UK
Contact:

Post 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.
Halo 2 Prophet - Skin with ease with the simple 3D point and click interface.
Halo 3 Research Thread - Contribute to the research into Halo 3.
OwnZ joO




Articulatist 500

Posts: 980
Joined: Thu Nov 10, 2005 4:24 pm

Post 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.
User avatar
Prey




Connoisseur Snitch! Pyre Articulatist 500

Posts: 1026
Joined: Wed Dec 27, 2006 6:49 am
Location: UK
Contact:

Post 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.
Halo 2 Prophet - Skin with ease with the simple 3D point and click interface.
Halo 3 Research Thread - Contribute to the research into Halo 3.
OwnZ joO




Articulatist 500

Posts: 980
Joined: Thu Nov 10, 2005 4:24 pm

Post 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.
Post Reply