C# のチェックボックス

PHPz
リリース: 2024-09-03 15:27:36
オリジナル
877 人が閲覧しました

CheckBox は、ユーザーがオプションのリストから 1 つまたは複数の選択をできるようにするコントロールです。 C# では、System.Windows.Forms 名前空間の CheckBox クラスを使用して、チェックボックス コントロールを操作します。これは Windows フォームの一部であり、ユーザーからの入力を取得するために使用されます。 true/false、yes/no などのオプションから選択するために使用することもできます。

ユーザーはチェックボックスをクリックして関連する項目を選択し、もう一度クリックして項目の選択を解除できます。画像やテキスト、あるいはその両方と一緒に使用できます。

チェックボックスの種類

チェックボックスは 2 つの異なる方法で作成できます:

  1. 設計時にフォーム デザイナを使用します。
  2. 実行時にコードで CheckBox クラスを使用します。

ToolBox からチェックボックス コントロールをドラッグし、Windows フォームにドロップすることで、設計時に checkBox を作成できます。次に、チェックボックス コントロールのプロパティに移動して、それを変更できます。

実行時にチェックボックスを作成するには、CheckBox クラスを使用する必要があります。

構文:

CheckBox check_box = new CheckBox();
ログイン後にコピー

この後、要件に従ってチェックボックスのプロパティを設定できます。

//setting location of checkbox
check_box.Location = new Point(300,150);
//setting height and width for checkbox
check_box.Height = 50;
check_box.Width = 50;
//setting text for checkbox
check_box.Text = “Yes”;
ログイン後にコピー

最後に、次を使用してこのチェックボックスを Windows フォームに追加する必要があります。

this.Controls.Add(check_box);
ログイン後にコピー

チェックボックスのプロパティ

C# には、チェックボックスのプロパティが多数用意されています。

Property Description
AllowDrop It is used to get or set a value that determines whether the checkbox control can accept data that a user drags onto it.
Appearance It is used to get or set a value that determines the appearance of the checkbox control.
AutoCheck It is used to get or set a value that determines whether the values of the Checked or CheckState properties or the appearance of the checkbox are automatically changed when the checkbox is clicked.
AutoSize It is used to get or set a value that determines whether the checkbox control resizes based on its content.
BackColor It is used to get or set the background color of the checkbox control.
BackGroundImage It is used to get or set the background image displayed in the checkbox control.
CanFocus It is used to get a value that determines whether the checkbox control can receive focus.
Checked It is used to get or set a value that determines whether the checkbox control is in the checked state.
CheckState It is used to get or set the state of the checkbox.
DefaultSize It is used to get the default size of the checkbox control.
Enabled It is used to get or set a value that determines whether the checkbox control can respond to user interaction.
Focused It is used to get a value that determines whether the checkbox control has input focus.
Font It is used to get or set the font of text displayed by the checkbox control.
ForeColor It is used to get or set the foreground color of the checkbox control.
Height It is used to get or set the height of the checkbox control.
Image It is used to get or set the image which is displayed on the checkbox control.
Location It is used to get or set the coordinates of the upper left corner of the control relative to the upper left corner of its parent container.
Margin It is used to get or set the space between controls.
Name It is used to get or set the name of the checkbox control.
Size It is used to get or set the height and width of the checkbox control.
Text It is used to get or set the text associated with the checkbox control.
ThreeState It is used to get or set a value that determines whether the checkbox will allow three check states instead of two.
Width It is used to get or set the width of the checkbox control.

CheckBox Events

Let us see some important events for the CheckBox provided by C#:

Event Description
CheckedChanged This event occurs when the value of Checked property changes.
CheckStateChanged This event occurs when the value of CheckState property changes.
Click This event occurs when the checkbox is clicked.
GotFocus This event occurs when the checkbox receives focus.
Leave This event occurs when the input focus leaves the checkbox.
LostFocus This event occurs when the checkbox loses focus.
MouseClick This event occurs when the checkbox is clicked by the mouse.
MouseDoubleClick This event occurs when the checkbox is double-clicked by a mouse.
TextChanged This event occurs when the value of Text property changes.

Implementation of CheckBox in C#

Below is an example of how to implement the checkbox in c #

Example:

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class CheckBoxDemo : Form
{
public CheckBoxDemo()
{
InitializeComponent();
}
private void CheckBoxDemo_Load(object sender, EventArgs e)
{
//Creating and setting properties of Label
Label label = new Label();
label.Text = "Select your technical skills";
label.AutoSize = true;
label.Location = new Point(192, 77);
label.Font = new Font("Microsoft Sans Serif", 11);
//Adding label to form
this.Controls.Add(label);
//Creating and setting properties of CheckBox
CheckBox checkbox1 = new CheckBox();
checkbox1.Location = new Point(195, 111);
checkbox1.Text = "C";
//Adding checkbox to form
this.Controls.Add(checkbox1);
CheckBox checkbox2 = new CheckBox();
checkbox2.Location = new Point(195, 156);
checkbox2.Text = "C++";
this.Controls.Add(checkbox2);
CheckBox checkbox3 = new CheckBox();
checkbox3.Location = new Point(195, 195);
checkbox3.Text = "C#";
this.Controls.Add(checkbox3);
CheckBox checkbox4 = new CheckBox();
checkbox4.Location = new Point(195, 235);
checkbox4.Text = "JAVA";
this.Controls.Add(checkbox4);
CheckBox checkbox5 = new CheckBox();
checkbox5.Location = new Point(195, 275);
checkbox5.Text = "HTML";
this.Controls.Add(checkbox5);
}
}
}
ログイン後にコピー

Output:

C# のチェックボックス

Conclusion – Checkbox in C#

A checkbox in C# can also have an indeterminate state. This can be achieved by setting the CheckState property to ‘Indeterminate. It is a state between ‘Yes’ and ‘No’ in which the checkbox will neither be checked nor unchecked.

C# のチェックボックス

以上がC# のチェックボックスの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!