通过


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

属性值

true 如果控件可以响应用户交互,则为否则,为 false. 默认值为 true

示例

下面的代码示例创建并 GroupBox 设置其一些常见属性。 该示例在 TextBox 组框中创建并设置它 Location 。 接下来,它将组框的属性设置为 Text 组框,并将组框停靠在窗体顶部。 最后,它通过将属性false设置为Enabled禁用组框来禁用组框,这将导致禁用组框中包含的所有控件。

   // 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设置为不禁用应用程序的控制框或阻止应用程序窗口接收焦点。

当容器控件已将其启用属性设置为 false时,也会禁用其包含的所有控件。 例如,如果用户单击禁用 GroupBox 的控件中包含的任何控件,则不会引发任何事件。

注释

禁用可滚动控件时,也会禁用滚动条。 例如,禁用的多行文本框无法滚动以显示所有文本行。

适用于

另请参阅