Control.Enabled 屬性

定義

取得或設定值,指出控制項是否可回應使用者互動。Gets or sets a value indicating whether the control can respond to user interaction.

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

屬性值

如果控制項可以回應使用者互動,則為 true,否則為 falsetrue if the control can respond to user interaction; otherwise, false. 預設為 trueThe default is true.

範例

下列程式碼範例會建立 GroupBox,並設定其一些通用屬性。The following code example creates a GroupBox and sets some of its common properties. 此範例會建立 TextBox,並在群組方塊內設定其 LocationThe example creates a TextBox and sets its Location within the group box. 接下來,它會設定 [群組] 方塊的 [Text] 屬性,並將群組方塊停駐在表單的頂端。Next, it sets the Text property of the group box, and docks the group box to the top of the form. 最後,它會將 [Enabled] 屬性設為 false 來停用群組方塊,這會停用群組方塊內包含的所有控制項。Lastly, it disables the group box by setting the Enabled property to false, which causes all controls contained within the group box to be disabled.

   // 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 屬性,您可以在執行時間啟用或停用控制項。With the Enabled property, you can enable or disable controls at run time. 例如,您可以停用不適用於應用程式目前狀態的控制項。For example, you can disable controls that do not apply to the current state of the application. 您也可以停用控制項來限制其使用。You can also disable a control to restrict its use. 例如,您可以停用按鈕,以防止使用者按一下它。For example, a button can be disabled to prevent the user from clicking it. 如果控制項已停用,就無法加以選取。If a control is disabled, it cannot be selected.

重要

Enabled 屬性設定為 false,並不會停用應用程式的控制方塊,或防止應用程式視窗接收焦點。Setting the Enabled property to false does not disable the application's control box or prevent the application window from receiving the focus.

當容器控制項的 enabled 屬性設定為 false 時,也會停用其包含的所有控制項。When a container control has its enabled property set to false, all its contained controls are disabled, as well. 例如,如果使用者按一下已停用的 GroupBox 控制項中包含的任何控制項,則不會引發任何事件。For example, if the user clicks on any of the controls contained in a disabled GroupBox control, no events are raised.

注意

當已停用可滾動控制項時,捲軸也會停用。When a scrollable control is disabled, the scroll bars are also disabled. 例如,停用的多行文字方塊無法滾動顯示所有的文字行。For example, a disabled multiline textbox is unable to scroll to display all the lines of text.

適用於

另請參閱