
C#のリストボックスの作成・選択・削除・追加の方法とは?
目次
C#のリストボックスについて
今回は、C#のリストボックスについて説明します。C#のリストボックスとは、リストデータを表示したり、選択する場合に使うコントロールのことです。
主にリストボックスの作成・選択・削除・追加の方法について紹介します。C#のリストボックスに興味のある方はぜひご覧ください。
リストの作成
C#で、初期値を設定してリストボックスを生成してみます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { ListBox listBox; public Form1() { listBox = new ListBox() { Location = new Point(10, 10), }; listBox.Items.Add(""Item1""); listBox.Items.Add(""Item2""); listBox.Items.Add(""Item3""); this.Controls.Add(listBox); this.Text = ""Form1""; } } } |
実行すると、リストボックスが表示されていることが分かります。
リストの選択
C#では、イベントハンドラで、選択したリストボックスの要素を取得できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { ListBox listBox; Label label; public Form1() { // リストボックス listBox = new ListBox() { Location = new Point(10, 10), }; listBox.Items.Add(""Item1""); listBox.Items.Add(""Item2""); listBox.Items.Add(""Item3""); // イベントハンドラ listBox.SelectedIndexChanged += new EventHandler(listBox_SelectedIndexChanged); // ラベル label = new Label() { Location = new Point(10, 100), AutoSize = true, }; this.Controls.Add(listBox); this.Controls.Add(label); this.Text = ""Form1""; } void listBox_SelectedIndexChanged(object sender, EventArgs e) { label.Text = string.Format( ""List[{0}]:{1} が選択されました。"", listBox.SelectedIndex, listBox.SelectedItem.ToString()); } } } |
実行すると、選択したリストの情報がLabelに表示されることが分かります。
リストの削除
C#でリストの要素を削除するには、以下のように記述します。
1 2 3 4 5 6 7 8 |
// インデックス指定で削除 listBox.Items.RemoveAt(0); // 選択されているアイテムを削除 listBox.Items.Remove(listBox.SelectedItem); // リストの名称から削除 listBox.Items.Remove(""Item1""); |
また、リストボックスの全削除は以下のように記述します。
1 2 3 |
listBox.Items.Clear(); |
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { ListBox listBox; Label label; public Form1() { // リストボックス listBox = new ListBox() { Location = new Point(10, 10), }; listBox.Items.Add(""Item1""); listBox.Items.Add(""Item2""); listBox.Items.Add(""Item3""); // イベントハンドラ listBox.SelectedIndexChanged += new EventHandler(listBox_SelectedIndexChanged); // ラベル label = new Label() { Location = new Point(10, 100), AutoSize = true, }; // 初期化ボタン Button button_init = new Button() { Text = ""init"", Location = new Point(140, 10), }; button_init.Click += new EventHandler(button_init_Click); // 削除ボタン Button button_del = new Button() { Text = ""del"", Location = new Point(140, 40), }; button_del.Click += new EventHandler(button_del_Click); // 削除ボタン Button button_del_all = new Button() { Text = ""del all"", Location = new Point(140, 70), }; button_del_all.Click += new EventHandler(button_del_all_Click); this.Controls.Add(listBox); this.Controls.Add(label); this.Controls.Add(button_init); this.Controls.Add(button_del); this.Controls.Add(button_del_all); this.Text = ""Form1""; } void listBox_SelectedIndexChanged(object sender, EventArgs e) { if (listBox.SelectedIndex >= 0) { label.Text = string.Format( ""List[{0}]:{1} が選択されました。"", listBox.SelectedIndex, listBox.SelectedItem.ToString()); } } void button_init_Click(object sender, EventArgs e) { listBox.Items.Clear(); listBox.Items.Add(""Item1""); listBox.Items.Add(""Item2""); listBox.Items.Add(""Item3""); listBox.Refresh(); } void button_del_Click(object sender, EventArgs e) { listBox.Items.Remove(listBox.SelectedItem); listBox.SelectedIndex = -1; } void button_del_all_Click(object sender, EventArgs e) { listBox.Items.Clear(); listBox.SelectedIndex = -1; } } } |
リストの中から、選択した要素をdelボタンで削除します。「del all」ボタンをクリックすると、リストボックスの要素を全削除します。initボタンをクリックすると、初期状態に戻ります。
リストの追加
C#のAddメソッドで、リストボックスに要素を追加できます。
実際のソースコードを見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { ListBox listBox; Label label; TextBox textBox; public Form1() { // リストボックス listBox = new ListBox() { Location = new Point(10, 10), }; listBox.Items.Add(""Item1""); listBox.Items.Add(""Item2""); listBox.Items.Add(""Item3""); // イベントハンドラ listBox.SelectedIndexChanged += new EventHandler(listBox_SelectedIndexChanged); // テキストボックス textBox = new TextBox() { Location = new Point(10, 100), }; // ラベル label = new Label() { Location = new Point(10, 150), AutoSize = true, }; // 初期化ボタン Button button_init = new Button() { Text = ""init"", Location = new Point(140, 10), }; button_init.Click += new EventHandler(button_init_Click); // 削除ボタン Button button_del = new Button() { Text = ""del"", Location = new Point(140, 40), }; button_del.Click += new EventHandler(button_del_Click); // 削除ボタン Button button_del_all = new Button() { Text = ""del all"", Location = new Point(140, 70), }; button_del_all.Click += new EventHandler(button_del_all_Click); // 追加ボタン Button button_add = new Button() { Text = ""add"", Location = new Point(140, 100), }; button_add.Click += new EventHandler(button_add_Click); this.Controls.Add(listBox); this.Controls.Add(textBox); this.Controls.Add(label); this.Controls.Add(button_init); this.Controls.Add(button_del); this.Controls.Add(button_del_all); this.Controls.Add(button_add); this.Text = ""Form1""; } void listBox_SelectedIndexChanged(object sender, EventArgs e) { if (listBox.SelectedIndex >= 0) { label.Text = string.Format( ""List[{0}]:{1} が選択されました。"", listBox.SelectedIndex, listBox.SelectedItem.ToString()); } } void button_init_Click(object sender, EventArgs e) { listBox.Items.Clear(); listBox.Items.Add(""Item1""); listBox.Items.Add(""Item2""); listBox.Items.Add(""Item3""); listBox.Refresh(); textBox.Clear(); } void button_del_Click(object sender, EventArgs e) { listBox.Items.Remove(listBox.SelectedItem); listBox.SelectedIndex = -1; } void button_del_all_Click(object sender, EventArgs e) { listBox.Items.Clear(); listBox.SelectedIndex = -1; } void button_add_Click(object sender, EventArgs e) { if (textBox.TextLength > 0) { listBox.Items.Add(textBox.Text); label.Text = string.Format(""{0}が追加されました。"", textBox.Text); textBox.Clear(); } } } } |
実行すると、addボタンでTextBoxの文字列がリストボックスに追加されることが分かります。
このように、C#のAddメソッドで、リストボックスに要素を追加できます。
C#のソースコードを書いてみよう
リストボックスとは、リストデータを表示、選択する場合に使うコントロールのことです。C#でのリストボックスの作成・選択・削除・追加の方法について紹介しました。
ぜひご自身でC#のソースコードを書いて、理解を深めてください。