次のコントロールを使用してフォームを作成しました。
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.いずれかのコンボボックスが変更されたときに、データを再度取得します。
お役に立てば幸いです。