Button 类

表示 Windows 按钮控件。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class Button
    Inherits ButtonBase
    Implements IButtonControl
用法
Dim instance As Button
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class Button : ButtonBase, IButtonControl
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class Button : public ButtonBase, IButtonControl
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class Button extends ButtonBase implements IButtonControl
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class Button extends ButtonBase implements IButtonControl

备注

如果某个 Button 具有焦点,则可以使用鼠标、Enter 键或空格键单击该按钮。

设置 FormAcceptButtonCancelButton 属性,使用户能够通过按 Enter 或 Esc 键来单击按钮(即使该按钮没有焦点)。这使该窗体具有对话框的行为。

当使用 ShowDialog 方法显示一个窗体时,可以使用按钮的 DialogResult 属性指定 ShowDialog 的返回值。

可以更改按钮的外观。例如,要使它显示为 Web 风格的平面外观,请将 FlatStyle 属性设置为 FlatStyle.FlatFlatStyle 属性还可设置为 FlatStyle.Popup;当鼠标指针经过该按钮时,它不再是平面外观,而是将呈现标准的 Windows 按钮外观。

提示

如果具有焦点的控件接受并处理按 Enter 键的操作,则 Button 不处理此操作。例如,如果某个多行的 TextBox 控件或其他按钮具有焦点,则该控件将处理按 Enter 键的操作,而非“接受”按钮处理此操作。

示例

下面的代码示例创建一个 Button,将其 DialogResult 属性设置为 DialogResult.OK,并将其添加到 Form

Private Sub InitializeMyButton()
    ' Create and initialize a Button.
    Dim button1 As New Button()
    
    ' Set the button to return a value of OK when clicked.
    button1.DialogResult = DialogResult.OK
    
    ' Add the button to the form.
    Controls.Add(button1)
End Sub 'InitializeMyButton
private void InitializeMyButton()
 {
    // Create and initialize a Button.
    Button button1 = new Button();
 
    // Set the button to return a value of OK when clicked.
    button1.DialogResult = DialogResult.OK;
 
    // Add the button to the form.
    Controls.Add(button1);
 }
 
private:
   void InitializeMyButton()
   {
      // Create and initialize a Button.
      Button^ button1 = gcnew Button;
      
      // Set the button to return a value of OK when clicked.
      button1->DialogResult = ::DialogResult::OK;
      
      // Add the button to the form.
      Controls->Add( button1 );
   }
private void InitializeMyButton()
{
    // Create and initialize a Button.
    Button button1 = new Button();
    
    // Set the button to return a value of OK when clicked.
    button1.set_DialogResult(get_DialogResult().OK);
    
    // Add the button to the form.
    get_Controls().Add(button1);
} //InitializeMyButton
private function InitializeMyButton()
 {
    // Create and initialize a Button.
    var button1 : Button = new Button();
 
    // Set the button to return a value of OK when clicked.
    button1.DialogResult = System.Windows.Forms.DialogResult.OK;
 
    // Add the button to the form.
    Controls.Add(button1);
 }
 

继承层次结构

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
         System.Windows.Forms.ButtonBase
          System.Windows.Forms.Button

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Button 成员
System.Windows.Forms 命名空间
ButtonBase
RadioButton
CheckBox
IButtonControl