Skip to content

Instantly share code, notes, and snippets.

@mrgarita
Created October 31, 2017 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrgarita/847e945fd1726ce2e6ee45961e4ee4d2 to your computer and use it in GitHub Desktop.
Save mrgarita/847e945fd1726ce2e6ee45961e4ee4d2 to your computer and use it in GitHub Desktop.
C#:コンボボックスの使い方
using System;
using System.Windows.Forms;
namespace コンボボックスの使い方
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// コンボボックスにアイテムを追加
comboBox1.Items.Add("オレンジ");
comboBox1.Items.Add("赤");
comboBox1.Items.Add("黄色");
comboBox1.Items.Add("緑");
comboBox1.Items.Add("青");
// コンボボックスを読み取り専用にする
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
// 初期表示
label1.Text = "";
comboBox1.SelectedIndex = -1; // 初期表示なし
}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
string selectedItem = comboBox1.SelectedItem.ToString();
label1.Text = selectedItem + "が選択されました";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment