2013-02-08 29 views
-1

我是VB.Net的新手,感觉有些困惑。 我想要两个列表框,每个列表框中都有项目。在我的第一个列表框中,我有4个项目,在我的第二个列表框中,我有5个项目。我还添加了一个文本框,用于我想要存储在数组中的值。例如:如果我选择第一个文本框的第一个值,第二个文本框的第二个值并在文本框中键入“5”,则5将存储在(0,1)的阵列。多维数组和列表框(VB.Net)

然后,我希望我的第一个列表框中的每个项目的所有值都显示在标签中,第二个项目,第三个项目和第四个项目也是如此。我想我会需要一个循环。

我知道如何创建一个数组,以及如何将值存储在一个数组,但我似乎无法弄清楚如何得到它使用列表框和文本框的工作。

回答

1

我创建了一个窗体具有以下控件:

ComboBox1 
ComboBox2 
Button1 
TextBox1 

我已经添加代码到Form_Load和的button1_Click事件,并创造了单ComboBox_SelectedIndexChanged事件处理程序来处理这两个组合框指数的变化。

Public Class Form1 
    Private _array(,) As String 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     ReDim _array(0 To ComboBox1.Items.Count, 0 To ComboBox2.Items.Count) 
    End Sub 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
     Dim c1 As Integer = If(ComboBox1.SelectedIndex = -1, 0, ComboBox1.SelectedIndex) 
     Dim c2 As Integer = If(ComboBox2.SelectedIndex = -1, 0, ComboBox2.SelectedIndex) 
     Debug.Print(String.Format("Set ({0},{1}) to {2}", c1, c2, TextBox1.Text)) 
     _array(c1, c2) = TextBox1.Text 
    End Sub 

    Private Sub ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged, ComboBox2.SelectedIndexChanged 
     Dim c1 As Integer = If(ComboBox1.SelectedIndex = -1, 0, ComboBox1.SelectedIndex) 
     Dim c2 As Integer = If(ComboBox2.SelectedIndex = -1, 0, ComboBox2.SelectedIndex) 
     Debug.Print(String.Format("Get ({0},{1}) to {2}", c1, c2, TextBox1.Text)) 
     TextBox1.Text = _array(c1, c2) 
    End Sub 
End Class 

什么我展示的是:当加载窗体以匹配您的组合框元素的数量
1阵列调整。
2.将数据加载到数组中的事件(在本例中为按钮单击事件)。
3.当任一组合框改变时再次检索数据。

希望有所帮助。

+0

那么,这是如何为你工作的? –