
C#のコンボボックスとは?入力不可にする方法を解説!
目次
C#における入力不可なコンボボックスについて
今回は、「C#における入力不可なコンボボックス」について説明します。
C#は、プロパティを未設定の場合であれば、コンボボックスには文字を入力できます。また、deleteキーで削除することもできます。
このような入力をさせないために、コンボボックスを入力不可(readOnly)にする方法について紹介します。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 |
using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { ComboBox comboBox; public Form1() { // コンボボックス comboBox = new ComboBox(); comboBox.Location = new Point(20, 20); // アイテムの追加 comboBox.Items.AddRange(new string[] { ""Item1"", ""Item2"", ""Item3"" }); // 初期選択状態 comboBox.SelectedIndex = 0; this.Controls.Add(comboBox); this.Text = ""Form1""; } } } |
実行すると、コンボボックスに文字を入力したり、deleteキーで削除できることが分かります。
このような入力をさせない方法について、以降で説明します。
DropDownStyleプロパティにDropDownListを設定する
C#のコンボボックスについて「入力不可」にするためには、「DropDownStyleプロパティ」に「DropDownList」を設定します。
ここで用いる「DropDownStyleプロパティ」には、以下の3つが設定可能です。
・DropDown:入力可能なドロップダウンリスト(デフォルト)
・DropDownList:入力不可のドロップダウンリスト
・Simple:入力可能なマルチラインリスト
それぞれのStyleについて、実際のソースコードを見てみましょう。
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.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { Label label1, label2, label3; ComboBox comboBox1, comboBox2, comboBox3; public Form1() { this.Width = 500; label1 = new Label(); label1.Location = new Point(20, 20); label1.Text = ""DropDown""; label2 = new Label(); label2.Location = new Point(150, 20); label2.Text = ""DropDownList""; label3 = new Label(); label3.Location = new Point(280, 20); label3.Text = ""Simple""; // コンボボックスの要素 string[] items = new string[] { ""Item1"", ""Item2"", ""Item3"" }; comboBox1 = new ComboBox(); comboBox1.Location = new Point(20, 50); comboBox1.Items.AddRange(items); comboBox1.SelectedIndex = 0; // 入力可能 comboBox1.DropDownStyle = ComboBoxStyle.DropDown; comboBox2 = new ComboBox(); comboBox2.Location = new Point(150, 50); comboBox2.Items.AddRange(items); comboBox2.SelectedIndex = 0; // 入力不可 comboBox2.DropDownStyle = ComboBoxStyle.DropDownList; comboBox3 = new ComboBox(); comboBox3.Location = new Point(280, 50); comboBox3.Items.AddRange(items); comboBox3.SelectedIndex = 0; // 入力可能 comboBox3.DropDownStyle = ComboBoxStyle.Simple; this.Controls.Add(label1); this.Controls.Add(label2); this.Controls.Add(label3); this.Controls.Add(comboBox1); this.Controls.Add(comboBox2); this.Controls.Add(comboBox3); this.Text = ""Form1""; } } } |
このように、「DropDownList」のコンボボックスのみが入力不可になっていることが分かります。
Enabledプロパティにfalseを設定する
C#のコンボボックスについて「入力不可」にするためには、「Enabledプロパティ」に「false」を設定する方法があります。
実際のソースコードを見てみましょう。
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 |
using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { ComboBox comboBox; public Form1() { // コンボボックス comboBox = new ComboBox(); comboBox.Location = new Point(20, 20); // アイテムの追加 comboBox.Items.AddRange(new string[] { ""Item1"", ""Item2"", ""Item3"" }); // 初期選択状態 comboBox.SelectedIndex = 0; // 無効化 comboBox.Enabled = false; this.Controls.Add(comboBox); this.Text = ""Form1""; } } } |
コンボボックスが無効化されているので「入力不可」になっていて、選択することはできません。
KeyPressEventHandlerでキー入力を無効にする
C#のコンボボックスについて「入力不可」にするためには、「KeyPressEventHandler」で「キー入力を無効」にする方法があります。
実際のソースコードを見てみましょう。
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 |
using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { ComboBox comboBox; public Form1() { // コンボボックス comboBox = new ComboBox(); comboBox.Location = new Point(20, 20); // アイテムの追加 comboBox.Items.AddRange(new string[] { ""Item1"", ""Item2"", ""Item3"" }); // 初期選択状態 comboBox.SelectedIndex = 0; // 背景色をライトグレー comboBox.BackColor = Color.LightGray; // KeyPressのイベントハンドラ comboBox.KeyPress += new KeyPressEventHandler(comboBox_KeyPress); this.Controls.Add(comboBox); this.Text = ""Form1""; } void comboBox_KeyPress(object sender, KeyPressEventArgs e) { // KeyPressを無効にする e.Handled = true; } } } |
この方法では、「DropDownStyleプロパティ」は設定していませんが、コンボボックスへのキー入力を無効にしているので、入力不可となります。
C#のコンボボックスを入力不可にする方法を知ろう!
この記事では、「C#のコンボボックスを入力不可」にする方法についてご紹介しましたが、いかがでしたでしょうか。
プロパティの設定や、キー入力のイベントハンドラの設定で、入力不可のコンボボックスを実現できます。ぜひご自身でC#のソースコードを書いて、理解を深めてください。