Solved

VB.NET Retrieving GroupBox Value

Posted on 2004-03-28
10
1,508 Views
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?
0
Comment
Question by:yan_er04
  • 6
  • 4
10 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10698355
Use CheckBox.Checked Property:

if checkBox1.Checked = true then
    ...
0
 

Author Comment

by:yan_er04
ID: 10698363
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.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10698389
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.
0
 

Author Comment

by:yan_er04
ID: 10698408
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?
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10698427
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.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 48

Expert Comment

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

Author Comment

by:yan_er04
ID: 10698446
I am not asking to get one value. I was hoping for an array of values of the checked checkboxes.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10698505
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
                Else
                    nResult(nTag) = False
                End If
            End If
        Next

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

        For i = 0 To 2
            If nResult(i) = True Then
                s.Append("+")
            Else
                s.Append("-")
            End If
        Next

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

    End Sub
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 10698522
Another way - handle Click event for each checkbox and set appropriate array element to true or false.
0
 

Author Comment

by:yan_er04
ID: 10698552
Thanks a lot
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now