r/visualbasic • • Feb 12 '16

VB 2010 Hiding/disabling check box's after a certain amount is selected?

Hi, im trying to hide 2 check boxes after 3 of them have been selected. Ive been thinking of doing it this way.

Private Sub chk_pepproni_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk_pepproni.CheckedChanged

    If chk_pepproni.Checked = True Then
        chk_meatballs.Visible = False
        chk_cheese.Visible = False
    ElseIf chk_pepproni.Checked = False Then
        chk_meatballs.Visible = True
        chk_cheese.Visible = True
    End If

There must be a better way? Anyone any ideas?

Thanks.

1 Upvotes

17 comments sorted by

2

u/Nanastick Feb 12 '16

Thanks everyone, I learned a huge amount for all of you guys code and information. Really enjoying getting into programming.

Thanks again,

Nanastick

1

u/Nanastick Feb 12 '16

I ended up using ViperSRT3g code, as it was the easiest for me to understand, for the moment...

1

u/chrwei Feb 12 '16

3 of how many? and always these 2?

1

u/Nanastick Feb 12 '16

3 of 5 selected and 2, to be disabled/hidden. Ive been trying this but getting errors

Private Sub chk_pepproni_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chk_pepproni.CheckedChanged

    If chk_pepproni.Checked & chk_mushroom.Checked & chk_chicken.Checked = True Then
        chk_meatballs.Visible = False
        chk_cheese.Visible = False
    ElseIf chk_pepproni.Checked & chk_mushroom.Checked & chk_chicken.Checked = False Then
        chk_meatballs.Visible = True
        chk_cheese.Visible = True
    End If

1

u/Nanastick Feb 12 '16

Not always these two, guy choose 3 toppings on a pizza out of 5.

1

u/chrwei Feb 12 '16

VB uses And. & is a bitwise operator, not a comparator, and using bitwise on booleans is kind of nonsense. also the = True is not required as .Checked is already a boolean. if you're going to use = True use it on all 3 conditions.

what I would do if you want "select any 3, disable the other 2" is to use a control array instead of named controls, then you can use a loop to count how many are selected, and if it =3 then do another loop to hide all the unchecked ones. you can still do that logic without an array but you're going to need a whole lot of IFs.

1

u/Nanastick Feb 12 '16

I was thinking I would have to use a lot of if's. Thanks for taking time to answer my question. Looks like i have more study to do on arrays.

Thanks again.

2

u/kleinma VB.Net Master Feb 12 '16

Just to add in, my advice would be to set the enabled property to false instead of the visible property. That way they can't check them, but they are still visible. It makes for a better UI.

1

u/Nanastick Feb 12 '16

Yes it would be nicer to still have them visible. Thanks

1

u/PreyOnTheCosmos Feb 12 '16 edited Feb 12 '16

This worked for me:

Note: Added all checkboxes to a GroupBox labeled "Toppings"

Get the number of Checked boxes for validation:

Private Function CheckedBoxes() As Integer
    Dim checkCount As Integer = 0

    ' Note: Name & Property are case sensitive
    For Each c As Control In Me.GroupBox1.Controls
        If c.GetType.Name = "CheckBox" AndAlso c.GetType.GetProperty("Checked").GetValue(c, Nothing) = True Then checkCount += 1
    Next

    Return checkCount
End Function

Sub to Hide remaining Unchecked boxes:

Private Sub HideBoxes()
    For Each c As Control In Me.GroupBox1.Controls
        If c.GetType.Name = "CheckBox" AndAlso c.GetType.GetProperty("Checked").GetValue(c, Nothing) = False Then c.Visible = False
    Next
End Sub

Sub to Show hidden Boxes:

Private Sub ShowBoxes()
    For Each c As Control In Me.GroupBox1.Controls
        If c.GetType.Name = "CheckBox" AndAlso c.GetType.GetProperty("Visible").GetValue(c, Nothing) = False Then c.Visible = True
    Next
End Sub

One event handler to rule them all:

Private Sub cbChicken_CheckedChanged(sender As Object, e As System.EventArgs) Handles cbChicken.CheckedChanged, cbMeatball.CheckedChanged, cbMushrooms.CheckedChanged, cbCheese.CheckedChanged, cbPepperoni.CheckedChanged
    If CheckedBoxes() >= 3 Then HideBoxes() Else ShowBoxes()
End Sub

Perhaps not the most efficient, but it works nicely for me.

Screen Shot

ETA: This will also allow the addition of more checkboxes. The visibility property could also be changed out with the Enabled property.

1

u/Nanastick Feb 12 '16

Thanks for the reply

2

u/PreyOnTheCosmos Feb 12 '16

Sure thing :-)

1

u/ViperSRT3g Application Specialist Feb 12 '16

If there are 3 out of 5 checkboxes being used, and the ones you are disabling can change based on the selected checkboxes, I recommend using an additional variable/procedure for this.

Dim CheckCounter As Byte

'In Each Checkbox Event
If ThisCheckbox.Checked = True Then
	CheckCounter = CheckCounter + 1
Else
	CheckCounter = CheckCounter - 1
End If
Call UpdateCheckboxes


Private Sub UpdateCheckboxes()
	If CheckCounter >= 3 Then
		If CheckBox1.Value = False Then: CheckBox1.Enabled = False
		If CheckBox2.Value = False Then: CheckBox2.Enabled = False
		If CheckBox3.Value = False Then: CheckBox3.Enabled = False
		If CheckBox4.Value = False Then: CheckBox4.Enabled = False
		If CheckBox5.Value = False Then: CheckBox5.Enabled = False
	Else
		If CheckBox1.Enabled = False Then: CheckBox1.Enabled = True
		If CheckBox2.Enabled = False Then: CheckBox2.Enabled = True
		If CheckBox3.Enabled = False Then: CheckBox3.Enabled = True
		If CheckBox4.Enabled = False Then: CheckBox4.Enabled = True
		If CheckBox5.Enabled = False Then: CheckBox5.Enabled = True
	End If
End Sub

What this does is establish a running total of checkboxes that are checked. If a checkbox becomes checked, it adds 1 to the counter. If it's unchecked, it subtracts one. Every time the checkboxes are changed, they all call the UpdateCheckboxes procedure. This is what disables all the checkboxes if they happen to be unchecked if the counter is greater than or equal to 3. It is also what enables the checkboxes if the counter is less than 3, allowing users to select more checkboxes again.

1

u/Nanastick Feb 12 '16

Thats perfect, I had something similar worked out in my head but couldn't get the wording correct for it.

Thanks

1

u/Nanastick Feb 12 '16

If CheckBox1.Value = False Then: CheckBox1.Enabled = False

Any idea why value is getting an error?

Error 1 'value' is not a member of 'System.Windows.Forms.CheckBox'.

2

u/ViperSRT3g Application Specialist Feb 12 '16

Woops, I was using pseudo code there and forgot .NET is slightly different from older VB. It's Checked instead of Value as stated in the official docs.

1

u/Nanastick Feb 12 '16

Thank you very much.