Results 1 to 5 of 5

Thread: [RESOLVED] Having Trouble Getting Runtime Generated Checkbox State

  1. #1
    Junior Member
    Join Date
    Aug 12
    Location
    Las Vegas, NV
    Posts
    31

    Resolved [RESOLVED] Having Trouble Getting Runtime Generated Checkbox State

    Hey everybody. I am fairly new at VB 2010 and I am running into a bit of an issue that has caused me to come to a screeching halt on my current project...

    I have been able to generate controls at runtime and obtain values from them without trouble until I generated a Groupbox at runtime and filled it with Checkboxes (obviously at runtime). How can I get the values of each of the runtime generated Checkboxes from each of the runtime generated Groupboxes?

    Any help would be appreciated. Here is the code that I used to generate the controls at runtime. I am not quite sure how to go about retrieving the values. Typically I use:


    Me.Controls.Item(control name).Text (For my text box controls)

    But I am not sure how to get the values of a checkbox control if it is inside of another runtime generated groupbox control.



    Sorry that my code shows how much of a beginner that I really am....

    Code:
     Private Sub btnAddGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddGroup.Click
            Dim strIBResult As String
            Dim intCurrentXPosition As Integer = 25
            Dim intCurrentYPosition As Integer = 120
            Dim intCurrentCBYPosition As Integer = 15
            'Dim intCurrentLabelXPosition As Integer = intCurrentXPosition + 50
            Dim intGroupBoxWidth As Integer = 200
            Dim intGroupBoxHeight As Integer = 200
            Dim intCurrentRow As Integer = 0
            Dim intTotalRows As Integer = m_DataTableClass.Rows.Count
    
            strIBResult = InputBox("How many groups would you like to add?", "Add Groups", " ")
            If Not IsNumeric(strIBResult) Then
                MessageBox.Show("Add group entry invalid", "Attention")
                Exit Sub
            End If
    
            intAddGroups = CInt(strIBResult)
            intTotalGroups += intAddGroups
    
            Do While intCurrentGroup <= intTotalGroups
                Dim grpNewGroupBox = New GroupBox
                grpNewGroupBox.Name = "grpGroup" & intCurrentGroup
                grpNewGroupBox.Text = "Group " & intCurrentGroup
                grpNewGroupBox.Location = New Point(intCurrentXPosition, intCurrentYPosition)
                grpNewGroupBox.Size = New Size(intGroupBoxWidth, intGroupBoxHeight)
                grpNewGroupBox.AutoSize = True
    
                Do While intCurrentRow < intTotalRows
                    Dim chkClassCheckBox = New CheckBox
                    chkClassCheckBox.Name = "chkClass" & intCurrentGroup & intCurrentRow
                    chkClassCheckBox.Text = m_DataTableClass.Rows(intCurrentRow).Item("ClassName")
                    chkClassCheckBox.Location = New Point(intCurrentXPosition, intCurrentCBYPosition)
                    grpNewGroupBox.Controls.Add(chkClassCheckBox)
    
                    intCurrentCBYPosition += 30
                    intCurrentRow += 1
                Loop
    
                Me.Controls.Add(grpNewGroupBox)
                intCurrentRow = 0
                intCurrentGroup += 1
                intCurrentYPosition = intCurrentCBYPosition + intCurrentYPosition + 50
                intCurrentCBYPosition = 15
            Loop
    
            intCurrentGroup = intTotalGroups - 1
            intLastGroupBoxYPosition = intCurrentYPosition
        End Sub
    Thanks!

  2. #2
    .Net Member dday9's Avatar
    Join Date
    Mar 11
    Location
    South Louisiana
    Posts
    2,334

    Re: Having Trouble Getting Runtime Generated Checkbox State

    You're on the right track, Me.Controls.Item(Item) would get the item for the controls on the form. Groupboxes have the same thing so GroupBox.Controls.Item(Item) would work. Take a look at my example:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        'Declare the values at the form level
        Private gb As New GroupBox
        Private cb As CheckBox
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            'Set the groubbox's properties
            gb.Location = New Point(15, 15)
            gb.Text = "Groupbox to hold some checkboxes"
            gb.Size = New Size(300, 500)
    
            'Add it to Me.Controls
            Me.Controls.Add(gb)
    
            'Loop 9
            For i As Integer = 10 To 1 Step -1
                cb = New CheckBox
                cb.Text = "Checkbox" & i.ToString
                cb.Location = New Point(15, 25 * i)
                'Add the checkbox to the Groupbox's controls
                gb.Controls.Add(cb)
            Next
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            'Store a list of string
            Dim CheckedCheckBox As New List(Of String)
    
            'Enumerate through all the checkboxes in the groupbox
            For Each CheckBox In gb.Controls.OfType(Of CheckBox)()
                'If the checkbox is checked then...
                If CheckBox.Checked = True Then
                    'Add it to the list
                    CheckedCheckBox.Add(CheckBox.Text)
                End If
            Next
    
            'Display a messagebox for all the items in the list
            For i As Integer = CheckedCheckBox.Count - 1 To 0 Step -1
                MessageBox.Show(CheckedCheckBox(i))
            Next
    
            'Clear the list
            CheckedCheckBox.Clear()
        End Sub
    End Class
    Vb.Net Contributions:-Here-
    Game Contributions:-Here- New - 2d map creator
    XNA in Vb.Net Tutorials:-Here-

    Links:
    LegalShield | AUP

  3. #3
    Junior Member
    Join Date
    Aug 12
    Location
    Las Vegas, NV
    Posts
    31

    Re: Having Trouble Getting Runtime Generated Checkbox State

    Thanks for the quick reply!

    I was actually thinking about using a list, but I am still unsure of how to use all of its functions.

    What would the best direction be if I were using multiple runtime generated groupboxes (the quantity of groupboxes is determined by the entry in the input box), all populated with runtime generated checkboxes? Should I use a list to store the names of all of the group boxes as well? How would I go about accessing all of the controls inside each groupbox if it is part of a list? (I may be all wrong about the list in general though)

    Is there a way to access individual controls in runtime generated groupboxes like the following:

    Me.runtime_generated_groupbox.Controls.Item(runtime_generated_checkbox).[I'm not quite sure what property of the checkbox would be available here, checked? text?]

    I am assuming that I am pretty far out of the ballpark with this one...

    Sorry if you already covered this, and I misunderstood your code... Thanks again

  4. #4
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 08
    Location
    Oregon
    Posts
    5,067

    Re: Having Trouble Getting Runtime Generated Checkbox State

    You can use a Dictionary with the key as the GroupBox name and the value as a List(Of CheckBox). Before adding a GroupBox (as shown below) check to see if the name exists in the Dictionary, if not add or of course remove it and add. Although everything is hard coded in regards to the properties being set that can be dynamic. What the important thing is the code shows the concept.
    Code:
    Private Sub Button1_Click( _
       ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim GroupBoxNames As String() = {"GB1", "GB2", "GB3", "GB1"}
       Dim Dict As New Dictionary(Of String, List(Of CheckBox))
       For Each item In GroupBoxNames
          If Not Dict.ContainsKey(item) Then
             Dim GroupBox As New GroupBox With {.Name = item}
             Dim Items As New List(Of CheckBox)(New CheckBox() _
                { _
                   New CheckBox With {.Name = "C1", .Checked = True}, _
                   New CheckBox With {.Name = "C2", .Checked = False} _
                } _
             )
             Dict.Add(item, Items)
          End If
       Next
    
       For Each item In Dict
          Console.WriteLine("{0} - {1}", item.Key, item.Value.Count)
          For Each cb In item.Value
             If cb.Checked Then
                ' do something
             Else
                ' do something else
             End If
          Next
       Next
    
       Dict.Item("GB1").Item(0).Checked = False
       Dict.Item("GB1").Item(1).Checked = True
       ' Who is checked 
       Dim Result = (From T In Dict.Item("GB1") Where T.Checked = True Select T.Name).ToList
       For Each item In Result
          Console.WriteLine(item)
       Next
    End Sub

  5. #5
    Junior Member
    Join Date
    Aug 12
    Location
    Las Vegas, NV
    Posts
    31

    Resolved Re: Having Trouble Getting Runtime Generated Checkbox State

    You guys are awesome! Thank you for all of the help. I really appreciate it.

    I was in fact able to use the Dictionary and List Object, as well as a few For...Next loops to solve this issue.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •