Control.Enabled Control.Enabled Control.Enabled Control.Enabled Property

定義

取得或設定值,指出控制項是否可回應使用者互動。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,並設定其Location群組方塊內。The 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.

當容器控制項具有其已啟用的屬性設定為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.

適用於

另請參閱