We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


VB.NET Retrieving GroupBox Value

yan_er04 asked
Medium Priority
Last Modified: 2007-12-19
If i put a number of checkboxs on a groupbox, how can I retrieve the values of the checkbox that is checked?
Watch Question

Use CheckBox.Checked Property:

if checkBox1.Checked = true then


Isn't there a faster way?
Like groupbox.value or something?
I could use the checked property but that will mean I have to loop through all the checkboxes.

It doesn't matter that checkboxes belong to one group, each of them mat be checked or not, when you check one of them, others are not unchecked.
Possibly you need RadioButton. In any case, testing all Checked properties is not time-critical.


not time-critical, but considering that i have 40 over checkboxes, it's a lot of codes.
is that any type of dynamic looping i can use?

If you have number of checkboxes, it's impossible to get only one value for group. Group is not defined for checkboxes because when one of them is checked, other checkboxes are not unchecked automatically. So, having 5 checkboxes in the form you can get results: checked, checked, unchecked, checked, unchecked. Obviously, to read this result you need loop.
To have only one value for group use RadioButton with AutoCheck property set to true. If you don't want to extract result for group, handle all RadioButton clicks in group and set some variable to clicked RadioButton number. This variable contains what you need - number of checked radio button in group.

By the way, 40 over checkboxes is not time-critical. To write short code you can enumerate Form.Controls or Panel.Controls collection.


I am not asking to get one value. I was hoping for an array of values of the checked checkboxes.

Consider Windows form with 3 checkboxes: CheckBox1, CheckBox2, CheckBox3 and button Button1. Let's keep checkbox number in array in it's Tag property:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CheckBox1.Tag = 0
        CheckBox2.Tag = 1
        CheckBox3.Tag = 2
    End Sub

To extract all checkbox values in array by generic way:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim n As Integer
        Dim i As Integer
        Dim nResult(3) As Boolean
        Dim s As System.Text.StringBuilder
        Dim nTag As Integer

        ' Read results to array
        n = Me.Controls.Count

        For i = 0 To n - 1
            If TypeOf (Me.Controls(i)) Is CheckBox Then
                nTag = CType(Me.Controls(i).Tag, Integer)
                If CType(Me.Controls(i), CheckBox).Checked = True Then
                    nResult(nTag) = True
                    nResult(nTag) = False
                End If
            End If

        ' Show array contents
        s = New System.Text.StringBuilder

        For i = 0 To 2
            If nResult(i) = True Then
            End If

        MessageBox.Show(Me, s.ToString())

    End Sub
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview


Thanks a lot
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.