重新设计动态维数组

时间:2017-02-13 02:14:33

标签: arrays vb.net multidimensional-array treeview

所以,我真的不确定如何标题这个问题。我试过寻找答案,但也许我没有使用正确的术语。这是基本的想法:

我有一个表单,左侧包含一个TreeView,作为“选项”表单。根据TreeView中选择的节点,右侧将显示相应的Panel,其中包含所选设置的控件。由于我只是在设计这个表格的开始,我不知道我到底会有多少选项;因此,我不确定最终会有多少个小组。

选项屏幕
next property

最初,通过将所有Panel放在单个父Panel中并创建一个循环(或4)以将所有Control.Panel对象添加到Control Array来解决问题。这允许我动态地基于所选节点显示正确的Panel。问题是......我目前只有一个子级别(即Parent和Parent-Child节点),因此我的数组只需要是二维的。但是,如果我决定添加另一个级别(即Parent-Child-Child),我的数组将需要为3维。

Imports System.ComponentModel
Imports System.Text.RegularExpressions
Public Class frm_options
    Dim all_controls(,) As Control = {}         'Array of panel controls inside of panel_container.
    Private Sub frm_options_Load(sender As Object, e As EventArgs) Handles Me.Load
        'Fill (and resize as needed) the all_controls(,) array with the panel controls based on their x,y coordinates by name.
        'Arrange ALL panels to the correct location.
        Dim x As Integer = 0
        Dim y As Integer = 0
        For Each panel_control As Control In panel_container.Controls
            If TypeOf (panel_control) Is Panel Then
                'Find the total amount of x,y panels that we need to fit into our array.
                'Find x,y dimension that panel should fit into array based on name [i.e. panel_1_0(0,0), panel_1_1(0,1), panel_2_0(1,0), panel_2_1(1,1), etc.]
                Dim xy As MatchCollection = Regex.Matches(panel_control.Name, "panel_(\d)*_*(\d)")
                For Each m As Match In xy
                    For Each cx As Capture In m.Groups(1).Captures
                        'For every x
                        If (cx.Value > x) Then
                            x = cx.Value
                        End If
                        For Each cy As Capture In m.Groups(2).Captures
                            'For every y in x
                            If (cy.Value > y) Then
                                y = cy.Value
                            End If
                            'Because we start our panel naming convention at 1_0, we must subtract this from our X value but not Y.
                            ReDim Preserve all_controls(x - 1, y)
                            all_controls(cx.Value - 1, cy.Value) = panel_control
                        Next
                    Next
                Next
            End If
        Next
        'At this point, x = maximum x coordinates needed, y = maximum y coordinates needed and all_controls contains a list of Panels for the TreeView.
    End Sub
    Private Sub options_tree_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles options_tree.AfterSelect
        'All below examples assume there is a single parent and child node.
        'If there is a need for a child in a child node, change Level to > 1 (or however many childs needed)
        If (options_tree.SelectedNode.Level > 0) Then
            'If the selected node is a child node...
            For Each control_panel In all_controls
                If (control_panel IsNot Nothing) Then
                    If (control_panel Is all_controls(options_tree.SelectedNode.Parent.Index, options_tree.SelectedNode.Index + 1)) Then
                        control_panel.Visible = True
                    Else
                        control_panel.Visible = False
                    End If
                End If
            Next
        Else
            'If the selected node is a parent node...
            For Each control_panel As Control In all_controls
                If (control_panel IsNot Nothing) Then
                    If (control_panel Is all_controls(options_tree.SelectedNode.Index, 0)) Then
                        control_panel.Visible = True
                    Else
                        control_panel.Visible = False
                    End If
                End If
            Next
        End If
    End Sub

原谅我丑陋的编码......我确信有更简单的方法来实现这一点 - 但我只是编码作为一种爱好,并且从未接受任何课程。如您所见,此代码允许我根据需要添加任意数量的节点和相应的面板(只要我继续使用正确的命名约定),而无需更改任何代码来实现新控件。但是,如果我向TreeView添加另一个级别,我将需要修改代码以使用三维数组。

简而言之,我确信我应该使用完全不同的方法。如果是这样,会是什么?同样,我希望有一个代码,允许我向表单添加节点,级别和面板,而无需更改代码来控制这些元素。

编辑:通过我的研究,我查看了Collection类。我使用它的问题是我不认为我能够根据所选节点的索引引用要显示的面板。

0 个答案:

没有答案