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.

重要

将属性设置为false不会禁用应用程序的控制框,也不会阻止应用程序窗口接收焦点。 EnabledSetting 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.

适用于

另请参阅