Control.Enabled 屬性

定義

取得或設定值,指出控制項是否可回應使用者互動。

public:
 property bool Enabled { bool get(); void set(bool value); };
public bool Enabled { get; set; }
member this.Enabled : bool with get, set
Public Property Enabled As Boolean

屬性值

Boolean

如果控制項可以回應使用者互動,則為 true,否則為 false。 預設值為 true

範例

下列程式碼範例會建立 , GroupBox 並設定其一些通用屬性。 此範例會建立 , TextBox 並在群組方塊中設定其 Location 。 接下來,它會設定 Text 群組方塊的 屬性,並將群組方塊停駐在表單頂端。 最後,它會將 屬性設定 Enabledfalse 來停用群組方塊,這會導致停用群組方塊中包含的所有控制項。

   // Add a GroupBox to a form and set some of its common properties.
private:
   void AddMyGroupBox()
   {
      // Create a GroupBox and add a TextBox to it.
      GroupBox^ groupBox1 = gcnew GroupBox;
      TextBox^ textBox1 = gcnew TextBox;
      textBox1->Location = Point(15,15);
      groupBox1->Controls->Add( textBox1 );

      // Set the Text and Dock properties of the GroupBox.
      groupBox1->Text = "MyGroupBox";
      groupBox1->Dock = DockStyle::Top;

      // Disable the GroupBox (which disables all its child controls)
      groupBox1->Enabled = false;

      // Add the Groupbox to the form.
      this->Controls->Add( groupBox1 );
   }
// Add a GroupBox to a form and set some of its common properties.
private void AddMyGroupBox()
{
   // Create a GroupBox and add a TextBox to it.
   GroupBox groupBox1 = new GroupBox();
   TextBox textBox1 = new TextBox();
   textBox1.Location = new Point(15, 15);
   groupBox1.Controls.Add(textBox1);

   // Set the Text and Dock properties of the GroupBox.
   groupBox1.Text = "MyGroupBox";
   groupBox1.Dock = DockStyle.Top;

   // Disable the GroupBox (which disables all its child controls)
   groupBox1.Enabled = false;

   // Add the Groupbox to the form.
   this.Controls.Add(groupBox1);
}
' Add a GroupBox to a form and set some of its common properties.
Private Sub AddMyGroupBox()
   ' Create a GroupBox and add a TextBox to it.
   Dim groupBox1 As New GroupBox()
   Dim textBox1 As New TextBox()
   textBox1.Location = New Point(15, 15)
   groupBox1.Controls.Add(textBox1)
   
   ' Set the Text and Dock properties of the GroupBox.
   groupBox1.Text = "MyGroupBox"
   groupBox1.Dock = DockStyle.Top
   
   ' Disable the GroupBox (which disables all its child controls)
   groupBox1.Enabled = False
   
   ' Add the Groupbox to the form.
   Me.Controls.Add(groupBox1)
End Sub

備註

Enabled使用 屬性,您可以在執行時間啟用或停用控制項。 例如,您可以停用不適用於應用程式目前狀態的控制項。 您也可以停用控制項來限制其使用。 例如,可以停用按鈕以防止使用者按一下按鈕。 如果控制項已停用,則無法選取它。

重要

Enabled 屬性設定為 false 不會停用應用程式的控制方塊,或防止應用程式視窗接收焦點。

當容器控制項的 enabled 屬性設定為 false 時,也會停用其所有包含的控制項。 例如,如果使用者按一下停用 GroupBox 控制項中包含的任何控制項,則不會引發任何事件。

注意

當可捲動控制項停用時,捲軸也會停用。 例如,停用的多行文字方塊無法捲動以顯示所有文字行。

適用於

另請參閱