[コード]
using System;
using System.Windows.Forms;
class Sample
{
[STAThread]
public static void Main()
{
Application.Run(new MainForm());
}
}
class MainForm : Form
{
private ComboBox comboBox = new ComboBox();//コンボボックス
private Label lb = new Label(); //ラベル
private Button bt = new Button(); //ボタン
public MainForm()
{
//値の設定
comboBox.Items.Add("晴れ");
comboBox.Items.Add("曇り");
comboBox.Items.Add("雨");
lb.Text = "";
lb.AutoSize = true;
lb.Top = comboBox.Bottom;
bt.Text = "値を取得";
bt.AutoSize = true;
bt.Top = lb.Bottom;
this.Controls.Add(comboBox);
this.Controls.Add(lb);
this.Controls.Add(bt);
bt.Click += new EventHandler(bt_Click);
}
public void bt_Click(Object sender, EventArgs e)
{
lb.Text = "";
//値を取得
lb.Text += comboBox.SelectedItem.ToString() ;
}
}
[説明]
コンボボックスの値の設定には、addメソッドをつかう。
「 comboBox.Items.Add("晴れ");」
値の取得には、「SelectedItem」をつかう。
「lb.Text += comboBox.SelectedItem.ToString() ;」
なお、Textプロパティで値を取得することもできる。