C#中动作类型控件应用实例
动作类型控件
使用动作类控件,用户可以与应用程序进行交互,这些控件可以帮助用户在应用程序中执行特定任务。动作类控件包括Button、ToolStrip、MenuStrip和ConText MenuStrip控件等。
1.Button控件功能
Button控件允许用户通过单击来执行各种操作。当Button控件被单击时,它看起来像是被按下,然后被释放。当用户单击Button控件时,即调用其Click事件执行各种操作。图1所示为Button控件。

图1 Button控件
2.Button控件属性
Button控件常用属性及说明如表1所示。

表1 Button控件常用属性及说明
3.Button控件方法
PerformClick方法:该方法引发Button控件的Click事件。当需要在用户没有单击按钮时也执行Click事件中代码时,可以使用此方法。
4.Button控件事件
Click事件:该事件主要用于Button控件的默认事件和主要事件,单击按钮时触发该事件。
示例
Click事件的使用
本示例中,当程序运行时,单击【清空】按钮,清空文本框中内容。示例运行结果如图2所示。

图2 Click事件
程序主要代码如下:
单击【清空】按钮,清空文本框中内容。【清空】按钮的Click事件代码如下:
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.Clear();
this.textBox2.Clear();
}
完整程序代码如下:
★★★★★主程序文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _8_03
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmText());
}
}
}
★★★★★Form1窗体设计文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _8_03
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
★★★★★Form1窗体代码文件完整程序代码★★★★★
namespace _8_03
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Text = "Form1";
}
#endregion
}
}
★★★★★frmText窗体设计文件完整程序代码★★★★★