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.

当容器控件具有其 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.

适用于

另请参阅