C#を使ったトグルボタンの作成方法について詳しく解説!

- システム
エンジニア - トルグボタンがうまく作成できず困っています。
- プロジェクト
マネージャー - では実際のソースコードを見ながらチェックしていきましょう。
C#のトグルボタンについて
今回は、C#のトグルボタンについてご説明します。トグルボタンとは、クリックするとへこみ、もう一度クリックすると元に戻るボタンのことです。
C#では、CheckBoxやRadioButton、ToolStripButtonをトグルボタンにすることができます。C#のトグルボタンに興味のある方はぜひご覧ください。
トグルボタン(CheckBox)
C#では、CheckBoxコントロールのAppearanceプロパティをButtonにすることで、トグルボタンにできます。
実際のソースコードを見てみましょう。
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 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { CheckBox checkBox; public Form1() { this.Load += new EventHandler(Form1_Load); } private void Form1_Load(object sender, EventArgs e) { checkBox = new CheckBox(); checkBox.Location = new Point(10, 10); checkBox.Text = "checkBox"; checkBox.TextAlign = ContentAlignment.MiddleCenter; // トグルボタンにする checkBox.Appearance = Appearance.Button; this.Controls.Add(checkBox); } } } |
“checkBox”ボタンが表示され、トグルボタンになっていることが分かります。このように、C#ではCheckBoxコントロールのAppearanceプロパティをButtonにすることで、トグルボタンにできます。
CheckBoxイベント
トグルボタンになっていることが分かりづらい可能性があるので、イベントハンドラを利用して分かりやすくしてみます。
実際のソースコードを見てみましょう。
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 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { CheckBox checkBox; public Form1() { this.Load += new EventHandler(Form1_Load); } private void Form1_Load(object sender, EventArgs e) { checkBox = new CheckBox(); checkBox.Location = new Point(10, 10); checkBox.Text = "checkBox off"; checkBox.TextAlign = ContentAlignment.MiddleCenter; // トグルボタンにする checkBox.Appearance = Appearance.Button; checkBox.CheckedChanged += new EventHandler(CheckBox_CheckedChanged); this.Controls.Add(checkBox); } private void CheckBox_CheckedChanged(object sender, EventArgs e) { if (checkBox.Checked) { // ボタンの背景色と文字列を変更 checkBox.BackColor = Color.Blue; checkBox.Text = "checkBox on"; } else { // ボタンの背景色と文字列を変更 checkBox.BackColor = Color.Red; checkBox.Text = "checkBox off"; } } } } |
ボタンの背景色と文字列が変更されることが分かります。
トグルボタン(RadioButton)
C#では、RadioButtonコントロールのAppearanceプロパティをButtonにすることで、トグルボタンにできます。
実際のソースコードを見てみましょう。
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 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { GroupBox groupBox; RadioButton radioButton1, radioButton2; public Form1() { this.Load += new EventHandler(Form1_Load); } private void Form1_Load(object sender, EventArgs e) { radioButton1 = new RadioButton(); radioButton1.Location = new Point(20, 20); radioButton1.Text = "Button1"; radioButton1.TextAlign = ContentAlignment.MiddleCenter; // トグルボタンにする radioButton1.Appearance = Appearance.Button; radioButton2 = new RadioButton(); radioButton2.Location = new Point(20, 50); radioButton2.Text = "Button2"; radioButton2.TextAlign = ContentAlignment.MiddleCenter; // トグルボタンにする radioButton2.Appearance = Appearance.Button; groupBox = new GroupBox(); groupBox.Location = new Point(10, 10); groupBox.Text = "radioButton"; groupBox.Controls.Add(radioButton1); groupBox.Controls.Add(radioButton2); this.Controls.Add(groupBox); } } } |
ラジオボタンをグループ化し、トグルボタンとしています。
このように、C#ではRadioButtonコントロールのAppearanceプロパティをButtonにすることで、トグルボタンにできます。
RadioButtonイベント
RadioButtonでは、CheckedChangedイベントが2回発生しますので注意が必要です。
実際のソースコードを見てみましょう。
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 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { GroupBox groupBox; RadioButton radioButton1, radioButton2; public Form1() { this.Load += new EventHandler(Form1_Load); } private void Form1_Load(object sender, EventArgs e) { radioButton1 = new RadioButton(); radioButton1.Location = new Point(20, 20); radioButton1.Text = "Button1"; radioButton1.TextAlign = ContentAlignment.MiddleCenter; // トグルボタンにする radioButton1.Appearance = Appearance.Button; radioButton1.CheckedChanged += new EventHandler(RadioButton1_CheckedChanged); radioButton2 = new RadioButton(); radioButton2.Location = new Point(20, 50); radioButton2.Text = "Button2"; radioButton2.TextAlign = ContentAlignment.MiddleCenter; // トグルボタンにする radioButton2.Appearance = Appearance.Button; radioButton2.CheckedChanged += new EventHandler(RadioButton2_CheckedChanged); groupBox = new GroupBox(); groupBox.Location = new Point(10, 10); groupBox.Text = "radioButton"; groupBox.Controls.Add(radioButton1); groupBox.Controls.Add(radioButton2); this.Controls.Add(groupBox); } private void RadioButton1_CheckedChanged(object sender, EventArgs e) { MessageBox.Show("button1 changed"); } private void RadioButton2_CheckedChanged(object sender, EventArgs e) { MessageBox.Show("button2 changed"); } } } |
ボタン選択を繰り返すと、radioButton1・radioButton2それぞれの選択状態が同時に変化しますので、イベントが2回発生することが分かります。
これを回避するには、チェック状態を見る必要があります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
private void RadioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked) { MessageBox.Show("button1 checked"); } } private void RadioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked) { MessageBox.Show("button2 checked"); } } |
トグルボタン(ToolStripButton)
C#では、ToolStripButtonコントロールもトグルボタンにできます。
実際のソースコードを見てみましょう。
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 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { ToolStrip toolStrip; ToolStripButton toolStripButton; public Form1() { this.Load += new EventHandler(Form1_Load); } private void Form1_Load(object sender, EventArgs e) { toolStrip = new ToolStrip(); toolStripButton = new ToolStripButton(); toolStrip.SuspendLayout(); this.SuspendLayout(); toolStrip.Items.Add(toolStripButton); toolStrip.Location = new Point(0, 0); toolStrip.Name = "toolStrip1"; toolStrip.TabIndex = 0; toolStrip.Text = "toolStrip1"; toolStripButton.Name = "toolStripButton"; toolStripButton.Text = "&button"; toolStripButton.TextAlign = ContentAlignment.MiddleCenter; toolStripButton.Click += new EventHandler(ToolStripButton_Click); this.Controls.Add(toolStrip); this.Name = "Form1"; this.toolStrip.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } private void ToolStripButton_Click(object sender, EventArgs e) { // チェック状態を反転する toolStripButton.Checked = !toolStripButton.Checked; } } } |
ToolStripButtonがトグルボタンになっていることが分かります。
また、CheckOnClickをtrueにすれば、イベントハンドラを作成する必要はありません。
1 2 3 4 |
//toolStripButton.Click += new EventHandler(ToolStripButton_Click); toolStripButton.CheckOnClick = true; |
このように、C#ではToolStripButtonコントロールもトグルボタンにできます。
- システム
エンジニア - 実際のソースコードを見て、つまづいていた部分が分かりました。
- プロジェクト
マネージャー - 理解いただけて何よりです。CheckOnClicをTuruにすることも重要です。では実践してみましょう。
C#でトグルボタンを作成してみましょう
いかがでしたでしょうか。C#でCheckBoxやRadioButton、ToolStripButtonをトグルボタンにする方法を紹介しました。ぜひご自身で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万円東京都新宿区(西新宿駅)