Solved

Create Array Dynamically

Posted on 2006-06-23
2
269 Views
Last Modified: 2008-02-01
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
Comment
Question by:Gunit2507
2 Comments
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 16973752
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 16973784
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

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

Suggested Solutions

Title # Comments Views Activity
Get list of word ducuments in a folder 10 35
Visual Studio editor? 1 38
Iterate a dictionnary to change values 4 53
Opening and Closing Connections and Data Adapters 10 42
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

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

21 Experts available now in Live!

Get 1:1 Help Now