C#のリストボックスの作成・選択・削除・追加の方法とは?

- システム
エンジニア - 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#のソースコードを書いて、理解を深めてください。
FEnet.NETナビ・.NETコラムは株式会社オープンアップシステムが運営しています。
株式会社オープンアップシステムはこんな会社です
秋葉原オフィスには株式会社オープンアップシステムをはじめグループのIT企業が集結!
数多くのエンジニアが集まります。

-
スマホアプリから業務系システムまで
スマホアプリから業務系システムまで開発案件多数。システムエンジニア・プログラマーとしての多彩なキャリアパスがあります。
-
充実した研修制度
毎年、IT技術のトレンドや社員の要望に合わせて、カリキュラムを刷新し展開しています。社内講師の丁寧なサポートを受けながら、自分のペースで学ぶことができます。
-
資格取得を応援
スキルアップしたい社員を応援するために資格取得一時金制度を設けています。受験料(実費)と合わせて資格レベルに合わせた最大10万円の一時金も支給しています。
-
東証プライム上場企業グループ
オープンアップシステムは東証プライム上場「株式会社夢真ビーネックスグループ」のグループ企業です。
安定した経営基盤とグループ間のスムーズな連携でコロナ禍でも安定した雇用を実現させています。
株式会社オープンアップシステムに興味を持った方へ
株式会社オープンアップシステムでは、開発系エンジニア・プログラマを募集しています。
年収をアップしたい!スキルアップしたい!大手の上流案件にチャレンジしたい!
まずは話だけでも聞いてみたい場合もOK。お気軽にご登録ください。


C#新着案件New Job
-
システム開発/東京都新宿区/【WEB面談可/C#経験者/20代前半の方活躍中/経験1年以上の方活躍中】/在宅勤務
月給29万~34万円東京都新宿区(新宿駅) -
システム開発/東京都新宿区/【WEB面談可/C#経験者/20代後半~40代の方活躍中/経験年数不問】/在宅勤務
月給41万~50万円東京都新宿区(新宿駅) -
デバック、テスト項目の作成/神奈川県横浜市/【WEB面談可/C#経験者/20代前半の方活躍中/経験1年以上の方活躍中】/在宅勤務
月給29万~34万円神奈川県横浜市(桜木町駅) -
デバック、テスト項目の作成/神奈川県横浜市/【WEB面談可/C#経験者/20代後半~40代の方活躍中/経験年数不問】/在宅勤務
月給41万~50万円神奈川県横浜市(桜木町駅) -
基幹システム開発導入/東京都新宿区/【WEB面談可/C#経験者/20代前半の方活躍中/経験1年以上の方活躍中】/在宅勤務
月給29万~34万円東京都新宿区(西新宿駅) -
基幹システム開発導入/東京都新宿区/【WEB面談可/C#経験者/20代後半~40代の方活躍中/経験年数不問】/在宅勤務
月給41万~50万円東京都新宿区(西新宿駅)