Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good morning everyone

I have a list of 12 CheckBoxes
(Called chk_Draw_1, chk_Draw_2 etc up to chk_Draw_12)
contained within a GroupBox (Called grp_Drawing_Types)
- none are Checked when the form is opened.

Once the user checks the boxes he requires, the program needs to determine which have been checked to simply get the Text property.

Using VBA I could write a bit of code like :
VB
For x = 1 To 12
    If Me.Controls("chk_Draw_" & x).Value = True Then
        UsedBoxes(x) = Me.Controls("chk_Draw_" & x).Caption
    End If
Next


Does anyone know if this type of routine would be possible in Vb.Net ?

I have tried various bits of code using
VB
for each ctrl as control in me.controls
  ....


but I keep making a complete hash of it.
Posted

You might simply create an array of check boxes, e.g.
VB
' fill properly the ellipses
Dim chkb As CheckBox() = {chk_Draw_1, chk_Draw_2, ..., chk_Draw_12}

and then use it.
 
Share this answer
 
Comments
Darrell de Wet 22-May-13 5:54am    
Interesting, I'll give it a shot. I still have much to learn.
Thanks for the advise.
I would suggest to add event handler[^] for each CheckBox and get checked checkboxes into List(of T) generic class[^].

VB
Public Class Form1

    Private WithEvents oChk As CheckBox
    Private CheckedCheckBoxes As List(Of CheckBox) = New List(Of CheckBox)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each chb As CheckBox In Me.GroupBox1.Controls
            If chb.Checked Then CheckedCheckBoxes.Add(chb)
        Next

        'do what you want to do...
        
    End Sub



    Private Sub oChk_CheckStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles oChk.CheckStateChanged
        Dim chb As CheckBox = CType(sender, CheckBox)
        MsgBox(chb.Name & " is Checked: " & chb.Checked)
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        For Each chb As CheckBox In Me.GroupBox1.Controls
            AddHandler chb.CheckStateChanged, AddressOf oChk_CheckStateChanged
        Next


    End Sub
End Class
 
Share this answer
 
Solved it myself (after much cursing and frustration)

VB
For Each ctrl In Me.grp_Drawing_Types.Controls
    If ctrl.checked = True Then
        TypeString = TypeString & ctrl.text & ", "
    End If
Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900