[Tips][ComboBox] 選択されている項目のインデックスを取得する

スポンサーリンク

本記事はWindowsアプリケーションのTipsです。


ComboBoxで現在選択されている項目のインデックスを取得するには、SelectedIndexプロパティを使用します。

下記にSelectedIndexプロパティを使用する例です。

VBの例

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim strData() As String = {"A", "B", "C", "D", "E"}
    'コンボボックスにアイテムを追加する
    ComboBox1.Items.AddRange(strData)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    '現在選択されているインデックスを取得する
    MessageBox.Show("選択された項目のインデックスは" & ComboBox1.SelectedIndex & "です")
End Sub

 

C#の例

private void Form1_Load(object sender, EventArgs e)
{
    string[] strData = { "A", "B", "C", "D", "E" };

    //コンボボックスにアイテムを追加する
    comboBox1.Items.AddRange(strData);
}

private void button1_Click(object sender, EventArgs e)
{
    //現在選択されているインデックスを取得する
    MessageBox.Show("選択された項目のインデックスは" + comboBox1.SelectedIndex + "です");
}

 

Please follow and like us:

コメント

タイトルとURLをコピーしました