Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

Create Array Dynamically

Hello, I have an unkown (it changes) number of group boxes on my form, with one combobox and two radio buttons (One "Ascending" and one "Descending"). What I am trying to do is store the values into a multidemsinal array. The form starts out with 4 groups, so this i use "Dim MyControls(0 To 3, 0 To 3)" to declare the array. Now how do I add the controls to the array

MyControls(?,0) = Should always be the groupbox
MyControls(?,1) = Should always be the combobox
MyControls(?,2) = Should always be the radiobutton that has the text of "Ascending"
MyControls(?,3) = Should always be the radiobutton that has the text of "Descending"

Currently if create the array "manually" it looks like this (maybe this would help?):
MyControls(0, 0) = Me.GroupOne : MyControls(1, 0) = Me.GroupTwo : MyControls(2, 0) = Me.GroupThree : MyControls(3, 0) = Me.GroupFour
MyControls(0, 1) = Me.ComboOne : MyControls(1, 1) = Me.ComboTwo : MyControls(2, 1) = Me.ComboThree : MyControls(3, 1) = Me.ComboFour
MyControls(0, 2) = Me.RadioOne : MyControls(1, 2) = Me.RadioThree : MyControls(2, 2) = Me.RadioFive : MyControls(3, 2) = Me.RadioSeven
MyControls(0, 3) = Me.RadioTwo : MyControls(1, 3) = Me.RadioFour : MyControls(2, 3) = Me.RadioSix : MyControls(3, 3) = Me.RadioEight

Thanks a lot for any help!
0
Gunit2507
Asked:
Gunit2507
1 Solution
 
ZeonFlashCommented:
Your problem turned out to be kind of fun, so I went a little wild with it :).  I've never had any luck whatsoever in using arrays in VB, especially when it comes to dynamically allocating them.  Instead, I made a dataset that holds your individual groups (controls in each groupbox).  It's a lot easier to add any dynamically created groupbox sets.  If it's not what you're looking for, no harm done...it was fun anyway!

    Private Enum ControlType
        GroupBox = 0
        ComboBox = 1
        RadioAsc = 2
        RadioDesc = 3
    End Enum

    Private dsGroups As New DataSet

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Set up the Groups dataset
        dsGroups.Tables.Add("Controls")
        With dsGroups.Tables("Controls")
            .Columns.Add("GroupBox")
            .Columns.Add("ComboBox")
            .Columns.Add("RadioAsc")
            .Columns.Add("RadioDesc")

            Dim primaryKey() As DataColumn = {.Columns("GroupBox")}
            .PrimaryKey = primaryKey

            .Columns("GroupBox").DataType = GetType(GroupBox)
            .Columns("ComboBox").DataType = GetType(ComboBox)
            .Columns("RadioAsc").DataType = GetType(RadioButton)
            .Columns("RadioDesc").DataType = GetType(RadioButton)
        End With

        'Loop through each control on the form and find each GroupBox
        For Each ctrlGroup As Control In Me.Controls
            If ctrlGroup.GetType Is GetType(GroupBox) Then
                'Add the Group "row" to the dataset
                dsGroups.Tables("Controls").Rows.Add(NewGroupSet(ctrlGroup))
            End If
        Next

        'Code to add new group box set to the form
        Dim grpNew As New GroupBox
        Dim grpCombo As New ComboBox
        Dim grpRadioAsc As New RadioButton
        Dim grpRadioDesc As New RadioButton
        grpNew.Controls.Add(grpCombo)
        grpNew.Controls.Add(grpRadioAsc)
        grpNew.Controls.Add(grpRadioDesc)
        Me.Controls.Add(grpNew)

        'Add the new GroupBox to the dataset
        dsGroups.Tables("Controls").Rows.Add(NewGroupSet(grpNew))
    End Sub

    Private Function NewGroupSet(ByVal grpBox As GroupBox) As DataRow
        Dim dr As DataRow = dsGroups.Tables("Controls").NewRow

        dr.Item("GroupBox") = grpBox

        For Each ctrl As Control In grpBox.Controls
            Select Case ctrl.GetType.ToString
                Case GetType(ComboBox).ToString
                    dr.Item("ComboBox") = ctrl
                Case GetType(RadioButton).ToString
                    If ctrl.Text = "Ascending" Then : dr.Item("RadioAsc") = ctrl
                    Else : dr.Item("RadioDesc") = ctrl
                    End If
            End Select
        Next

        Return dr
    End Function

    Private Function FindGrpControl(ByVal ctrlType As ControlType, ByVal grpToSearch As GroupBox) As Control
        Dim dr As DataRow = Nothing
        dr = dsGroups.Tables("Controls").Rows.Find(grpToSearch)

        If dr Is Nothing Then Return Nothing

        Select Case ctrlType
            Case ControlType.GroupBox
                Return dr.Item("GroupBox")
            Case ControlType.RadioAsc
                Return dr.Item("RadioAsc")
            Case ControlType.RadioDesc
                Return dr.Item("RadioDesc")
            Case ControlType.ComboBox
                Return dr.Item("ComboBox")
        End Select
    End Function
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If I understand correctly...then something like:

(the order of the "groups" is not guaranteed)

Public Class Form1

    Private MyControls(,) As Control

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ReDim MyControls(GroupBoxCount() - 1, 3)

        Dim count As Integer = -1
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is GroupBox Then
                count = count + 1
                MyControls(count, 0) = ctl
                For Each subCtl As Control In ctl.Controls
                    If TypeOf subCtl Is ComboBox Then
                        MyControls(count, 1) = subCtl
                    ElseIf TypeOf subCtl Is RadioButton Then
                        Select Case subCtl.Text
                            Case "Ascending"
                                MyControls(count, 2) = subCtl
                            Case "Descending"
                                MyControls(count, 3) = subCtl
                        End Select
                    End If
                Next
            End If
        Next

        For x As Integer = 0 To MyControls.GetUpperBound(0)
            Debug.WriteLine("Group " & x)
            For y As Integer = 0 To MyControls.GetUpperBound(1)
                Debug.WriteLine("(" & y & ") = " & MyControls(x, y).Name)
            Next
        Next
    End Sub

    Private Function GroupBoxCount() As Integer
        Dim count As Integer = 0
        For Each ctl As Control In Me.Controls
            If TypeOf ctl Is GroupBox Then
                count = count + 1
            End If
        Next
        Return count
    End Function

End Class
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now