本記事はWindowsアプリケーションのTipsです。
ComboBoxで現在選択されている項目を取得するには、SelectedItemプロパティを使用します。
下記は、現在選択されている項目を取得する例です。
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.SelectedItem.ToString())
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.SelectedItem.ToString());
}
Please follow and like us:

コメント