我创建了一个包含以下控件的表单:
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. 当任一组合框更改时再次检索数据。
希望有帮助。