📜  C#中的按钮

📅  最后修改于: 2021-05-29 21:19:46             🧑  作者: Mango

按钮是应用程序,软件或网页的重要组成部分。它允许用户与应用程序或软件进行交互。例如,如果用户要退出当前应用程序,则他/她单击退出按钮以关闭该应用程序。它可以根据您的程序要求执行许多操作,例如提交,上传,下载等。它可以具有不同的形状,大小,颜色等,并且您可以在不同的应用程序中重复使用它们。在.NET Framework中,Button类用于表示Windows按钮控件,并且它是从ButtonBase类继承的。它在System.Windows.Forms命名空间下定义。

在C#中,您可以使用两种不同的方法在Windows窗体上创建一个按钮:

1.设计时:这是创建按钮的最简单方法。使用以下步骤:

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:从“工具箱”中拖动“按钮”控件,并将其放在Windows窗体上。您可以根据需要在Windows窗体上的任意位置放置一个Button控件。
  • 步骤3:拖放之后,您将转到Button控件的属性来设置Button的属性。

2.运行时:比上述方法有些棘手。在这种方法中,您可以使用Button类创建自己的Button。

  • 步骤1:使用Button类提供的Button()构造函数创建一个按钮。
    // Creating Button using Button class
    Button MyButton = new Button();
    
  • 步骤2:创建Button之后,设置Button类提供的Button属性。
    // Set the location of the button
    Mybutton.Location = new Point(225, 198);
    
    // Set text inside the button
    Mybutton.Text = "Submit";
    
    // Set the AutoSize property of the button
     Mybutton.AutoSize = true;
    
    // Set the background color of the button
    Mybutton.BackColor = Color.LightBlue;
    
    // Set the padding of the button
    Mybutton.Padding = new Padding(6);
    
    // Set font of the text present in the button
    Mybutton.Font = new Font("French Script MT", 18);
    
  • 步骤3:最后,使用Add()方法将此按钮控件添加到窗体中。
    // Add this Button to form
    this.Controls.Add(Mybutton);
    

    例子:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp8 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
      
            // Creating and setting the properties of label
            Label l = new Label();
            l.AutoSize = true;
            l.Text = "Do you want to submit this project?";
            l.Location = new Point(222, 145);
            l.Font = new Font("French Script MT", 18);
            // Adding this label to form
            this.Controls.Add(l);
      
            // Creating and setting the properties of Button
            Button Mybutton = new Button();
            Mybutton.Location = new Point(225, 198);
            Mybutton.Text = "Submit";
            Mybutton.AutoSize = true;
            Mybutton.BackColor = Color.LightBlue;
            Mybutton.Padding = new Padding(6);
            Mybutton.Font = new Font("French Script MT", 18);
      
            // Adding this button to form
            this.Controls.Add(Mybutton);
      
            // Creating and setting the properties of Button
            Button Mybutton1 = new Button();
            Mybutton1.Location = new Point(360, 198);
            Mybutton1.Text = "Cancel";
            Mybutton1.AutoSize = true;
            Mybutton1.BackColor = Color.LightPink;
            Mybutton1.Padding = new Padding(6);
            Mybutton1.Font = new Font("French Script MT", 18);
      
            // Adding this button to form
            this.Controls.Add(Mybutton1);
        }
    }
    }
    

    输出:

按钮的重要属性

Property Description
BackColor Using BackColor property you can set the background color of the button.
BackgroundImage Using BackgroundImage poperty you can set the background image on the button.
AutoEllipsis Using AutoEllipsis property you can set a value which shows that whether the ellipsis character (…) appears at the right edge of the control which denotes that the button text extends beyond the specified length of the button.
AutoSize Using AutoSize property you can set a value which shows whether the button resizes based on its contents.
Enabled Using Enabled property you can set a value which shows whether the button can respond to user interaction.
Events Using Events property you can get the list of the event handlers that are applied on the given button.
Font Using Font property you can set the font of the button.
FontHeight Using FontHeight property you can set the height of the font.
ForeColor Using ForeColor property you can set the foreground color of the button.
Height Using Height property you can set the height of the button.
Image Using Image property you can set the image on the button.
Margin Using Margin property you can set the margin between controls.
Name Using Name property you can set the name of the button.
Padding Using Padding property you can set the padding within the button.
Visible Using Visible property you can set a value which shows whether the button and all its child buttons are displayed.

按键上的重要事件

Event Description
Click This event occur when the button is clicked.
DoubleClick This event occur when the user performs double click on the button.
Enter This event occur when the control is entered.
KeyPress This event occur when the character, or space, or backspace key is pressed while the control has focus.
Leave This event occur when the input focus leaves the control.
MouseClick This event occur when you click the mouse pointer on the button.
MouseDoubleClick This event occur when you double click the mouse pointer on the button.
MouseHover This event occur when the mouse pointer placed on the button.
MouseLeave This event occur when the mouse pointer leaves the button.