CheckBox クラス
アセンブリ: System.Web (system.web.dll 内)
構文
<ControlValuePropertyAttribute("Checked")> _ Public Class CheckBox Inherits WebControl Implements IPostBackDataHandler, ICheckBoxControl
[ControlValuePropertyAttribute("Checked")] public class CheckBox : WebControl, IPostBackDataHandler, ICheckBoxControl
[ControlValuePropertyAttribute(L"Checked")] public ref class CheckBox : public WebControl, IPostBackDataHandler, ICheckBoxControl
CheckBox を使用すると、true または false の状態を選択できます。
複数の CheckBox コントロールを使用する場合は、CheckBoxList コントロールを代替コントロールとして使用できます。このコントロールでは、便利なデータ連結機能を提供します。それぞれの CheckBox コントロールには強力なレイアウト制御機能が用意されています。
注意 |
---|
このコントロールは、ユーザー入力を表示するために使用できます。ユーザー入力には悪意のあるクライアント スクリプトが含まれている可能性があります。アプリケーションに表示する前に、クライアントから送信された実行スクリプト、SQL ステートメントなどのコードの情報はすべて検証してください。入力テキストをコントロールに表示する前に、検証コントロールを使用してユーザー入力を検証できます。ASP.NET には入力要求の検証機能があり、ユーザー入力の中のスクリプトおよび HTML をブロックできます。詳細については、「標準コントロールのセキュリティ保護」、「方法 : HTML エンコーディングを文字列に適用して Web アプリケーションをスクリプトによる攻略から保護する」、および「ASP.NET Web ページにおけるユーザー入力の検証」を参照してください。 |
ユーザー補助
このコントロールに既定でレンダリングされるマークアップは、Web Content Accessibility Guidelines (WCAG) 1.0 の優先度 1 ガイドラインなどのユーザー補助に関する標準に適合しない可能性があります。このコントロールのユーザー補助サポートの詳細については、「ASP.NET コントロールとユーザー補助」を参照してください。
CheckBox コントロールを使用して、売上合計に税額を含めるかどうかを指定する方法を次の例に示します。
メモ |
---|
次のコード サンプルはシングルファイル コード モデルを使用しており、分離コード ファイルに直接コピーされた場合は正常に動作しない可能性があります。このコード サンプルは、拡張子が .aspx の空のテキスト ファイルにコピーする必要があります。Web フォームのコード モデルの詳細については、「ASP.NET Web ページのコード モデル」を参照してください。 |
<%@ Page Language="VB" AutoEventWireup="True" %> <html> <head> <script runat="server"> Sub Check_Clicked(sender As Object, e As EventArgs) ' Calculate the subtotal and display the result in currency format. ' Include tax if the check box is selected. Message.Text = CalculateTotal(checkbox1.Checked).ToString("c") End Sub Sub Page_Load(sender As Object, e As EventArgs) ' Display the subtotal without tax when the page is first loaded. If Not IsPostBack Then ' Calculate the subtotal and display the result in currency format. Message.Text = CalculateTotal(false).ToString("c") End If End Sub Function CalculateTotal(Taxable As Boolean) As Double ' Calculate the subtotal for the example. Dim Result As Double = 1.99 + 2.99 + 3.99 ' Add tax, if applicable. If(Taxable) Result += Result * 0.086 End If Return Result End Function </script> </head> <body> <form runat="server"> <h3>CheckBox CheckedChanged Example</h3> Select whether to include tax in the subtotal. <br><br> <table border="1" cellpadding="5"> <tr> <th colspan="2"> Shopping cart </th> </tr> <tr> <td> Item 1 </td> <td> $1.99 </td> </tr> <tr> <td> Item 2 </td> <td> $2.99 </td> </tr> <tr> <td> Item 3 </td> <td> $3.99 </td> </tr> <tr> <td> <b>Subtotal</b> </td> <td> <asp:Label id="Message" runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:CheckBox id="checkbox1" runat="server" AutoPostBack="True" Text="Include 8.6% sales tax" TextAlign="Right" OnCheckedChanged="Check_Clicked"/> </td> </tr> </table> </form> </body> </html>
<%@ Page Language="C#" AutoEventWireup="True" %> <html> <head> <script runat="server"> void Check_Clicked(Object sender, EventArgs e) { // Calculate the subtotal and display the result in currency format. // Include tax if the check box is selected. Message.Text = CalculateTotal(checkbox1.Checked).ToString("c"); } void Page_Load(Object sender, EventArgs e) { // Display the subtotal without tax when the page is first loaded. if(!IsPostBack) { // Calculate the subtotal and display the result in currency format. Message.Text = CalculateTotal(false).ToString("c"); } } double CalculateTotal(bool Taxable) { // Calculate the subtotal for the example. double Result = 1.99 + 2.99 + 3.99; // Add tax, if applicable. if(Taxable) { Result += Result * 0.086; } return Result; } </script> </head> <body> <form runat="server"> <h3>CheckBox CheckedChanged Example</h3> Select whether to include tax in the subtotal. <br><br> <table border="1" cellpadding="5"> <tr> <th colspan="2"> Shopping cart </th> </tr> <tr> <td> Item 1 </td> <td> $1.99 </td> </tr> <tr> <td> Item 2 </td> <td> $2.99 </td> </tr> <tr> <td> Item 3 </td> <td> $3.99 </td> </tr> <tr> <td> <b>Subtotal</b> </td> <td> <asp:Label id="Message" runat="server"/> </td> </tr> <tr> <td colspan="2"> <asp:CheckBox id="checkbox1" runat="server" AutoPostBack="True" Text="Include 8.6% sales tax" TextAlign="Right" OnCheckedChanged="Check_Clicked"/> </td> </tr> </table> </form> </body> </html>
System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.CheckBox
System.Web.UI.WebControls.RadioButton
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
CheckBox メンバ
System.Web.UI.WebControls 名前空間
CheckBoxList
WebControl
その他の技術情報
CheckBox Web サーバー コントロールおよび CheckBoxList Web サーバー コントロール
CheckBox クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文
<ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _ Public Class CheckBox Inherits ButtonBase
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] public class CheckBox : ButtonBase
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] public ref class CheckBox : public ButtonBase
CheckBox を使用すると、ユーザーに true/false、yes/no などのオプションを提示できます。CheckBox コントロールは、イメージ、テキスト、またはその両方を表示できます。
CheckBox コントロールと RadioButton コントロールには似た機能があり、ユーザーがオプションのリストから選択できるようにします。CheckBox コントロールは、複数のオプションを組み合わせて選択できるようにします。これに対して RadioButton コントロールは、複数のオプションから 1 つだけを選択できるようにします。
Appearance プロパティは、CheckBox を通常のCheckBox で表示するのか、またはボタンで表示するのかを決定します。
ThreeState プロパティは、コントロールで 2 ステートと 3 ステートのどちらをサポートするのかを決定します。Checked プロパティは、2 ステートのCheckBox コントロールの値を取得または設定するために使用し、CheckState プロパティは、3 ステートのCheckBox コントロールの値を取得または設定するために使用します。
メモ |
---|
ThreeState プロパティが true に設定されている場合、Checked プロパティは、チェックされた状態と中間状態のどちらであるかを示す true を返します。 |
FlatStyle プロパティは、コントロールのスタイルと外観を決定します。FlatStyle プロパティが FlatStyle.System に設定されている場合は、ユーザーが使用しているオペレーティング システムによってコントロールの外観が決定されます。
メモ |
---|
FlatStyle プロパティが FlatStyle.System に設定されていると、CheckAlign プロパティの値は無視され、コントロールは ContentAlignment.MiddleLeft 値または ContentAlignment.MiddleRight 値を使用して整列および表示されます。CheckAlign プロパティの値が右揃えの 1 つに設定されている場合、コントロールは ContentAlignment.MiddleRight 値を使用して整列および表示されます。それ以外の場合は、ContentAlignment.MiddleLeft 値が使用されます。 |
中間状態とは、次のような状態です。RichTextBox で選択したテキストを太字にするかどうかを決定する CheckBox があるとします。テキストを選択して CheckBox をクリックすると、選択したテキストが太字になります。同様に、テキストを選択すると、選択したテキストが太字かどうかが CheckBox に表示されます。選択したテキストに太字と標準の両方のテキストがある場合、CheckBox は中間状態となります。
CheckBox を作成および初期化し、その外観をトグル ボタンにするコード例を次に示します。さらに、AutoCheck を false に設定し、トグル ボタンを Form に追加します。
Public Sub InstantiateMyCheckBox() ' Create and initialize a CheckBox. Dim checkBox1 As New CheckBox() ' Make the check box control appear as a toggle button. checkBox1.Appearance = Appearance.Button ' Turn off the update of the display on the click of the control. checkBox1.AutoCheck = False ' Add the check box control to the form. Controls.Add(checkBox1) End Sub 'InstantiateMyCheckBox
public void InstantiateMyCheckBox() { // Create and initialize a CheckBox. CheckBox checkBox1 = new CheckBox(); // Make the check box control appear as a toggle button. checkBox1.Appearance = Appearance.Button; // Turn off the update of the display on the click of the control. checkBox1.AutoCheck = false; // Add the check box control to the form. Controls.Add(checkBox1); }
public: void InstantiateMyCheckBox() { // Create and initialize a CheckBox. CheckBox^ checkBox1 = gcnew CheckBox; // Make the check box control appear as a toggle button. checkBox1->Appearance = Appearance::Button; // Turn off the update of the display on the click of the control. checkBox1->AutoCheck = false; // Add the check box control to the form. this->Controls->Add( checkBox1 ); }
public void InstantiateMyCheckBox() { // Create and initialize a CheckBox. CheckBox checkBox1 = new CheckBox(); // Make the check box control appear as a toggle button. checkBox1.set_Appearance(Appearance.Button); // Turn off the update of the display on the click of the control. checkBox1.set_AutoCheck(false); // Add the check box control to the form. get_Controls().Add(checkBox1); } //InstantiateMyCheckBox
public function InstantiateMyCheckBox() { // Create and initialize a CheckBox. var checkBox1 : CheckBox = new CheckBox(); // Make the check box control appear as a toggle button. checkBox1.Appearance = Appearance.Button; // Turn off the update of the display on the click of the control. checkBox1.AutoCheck = false; // Add the check box control to the form. Controls.Add(checkBox1); }
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ButtonBase
System.Windows.Forms.CheckBox
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- CheckBox クラスのページへのリンク