Solved

Create Array Dynamically

Posted on 2006-06-23
2
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

728 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