2.4.2用Visual Studio.Net建立Windows应用程序框架

以上所做的工作,都是一些固定的工作,可以使用Visual Studio.Net自动建立,下面介绍使用Visual Studio.Net创建Windows应用程相关C#教程序的具体步骤。

(1)运行Visual Studio.Net程序,出现如图1.2.2A界面。

(2)单击新建项目按钮,出现如图1.2.2B对话框。在项目类型(P)编辑框中选择Visual C#项目,在模板(T)编辑框中选Windows应用程序,在名称(N)编辑框中键入e2,在位置(L)编辑框中键入D:\csarp。也可以单击浏览按钮,在打开文件对话框中选择文件夹。单击确定按钮,创建项目。出现如图2.4.2A界面。生成一个空白窗体(Form1)。


图2.4.2A

(3)在e2文件夹中下有两个文件夹和8个文件,一般只修改Form1.cs文件。右击Form1窗体,在快捷菜单中选择菜单项查看代码(C),可打开Form1.cs文件。Visual Studio.Net生成的Foem1.cs文件如下,这是使用Visual Studio.Net创建Windows应用程序的最基本的形式。底色为黑色的字是作者增加的注解。

using System;//引入名字空间
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace e2//定义名字空间,///为解释
{//此处可定义其它类
/// 
/// Form1 的摘要说明。
/// 
public class Form1 : System.Windows.Forms.Form//Forme1类定义
{//此处可定义自己的变量,这些变量和运行程序同生命周期
/// 
/// 必需的设计器变量。
/// 
private System.ComponentModel.Container components = null;
public Form1()//构造函数
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();//此函数系统自动生成,不要修改,该函数做一些初始化工作
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//在构造函数增加自己的初始化代码,必须放在InitializeComponent()之后
}
/// 
/// 清理所有正在使用的资源。
/// 
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// 
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// 
private void InitializeComponent()
{//此函数系统自动生成,不要修改函数内容,函数做一些初始化工作
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";//this 是Form1窗体对象
this.Text = "Form1";
}
#endregion
/// 
/// 应用程序的主入口点。
/// 
[STAThread]
static void Main()//程序入口函数 ,一般不修改
{
Application.Run(new Form1());//主程序建立窗体运行
}//程序入口函数之后可定义自己的方法、属性等
}
}

(4)下边在窗体中增加一个按钮,并为按钮增加单击事件函数。单击图2.4.2A中标题为Forms.cs[设计]的窗口标签,返回标题为Forms.cs[设计]的窗口。向项目中添加控件需要使用工具箱窗口,若看不到,可以用菜单命令视图/工具箱打开这个窗口(见图2.4.2B左图)。选中工具箱窗口中Windows窗体类型下的Button条目,然后在标题为Forms.cs[设计]的窗口的Form1窗体中按下鼠标左键,拖动鼠标画出放置Button控件的位置,抬起鼠标左键,就将Button控件放到Form1窗体中。选中按钮控件,属性窗口(见图2.4.2B中图)显示按钮属性,其中左侧为属性名称,右侧为属性值,用属性窗口修改Button的Text属性值为:确定。单击属性窗体上的第4个图标,打开事件窗口(见图2.4.2B右图),显示Button控件所能响应的所有事件,其中左侧为事件名称,右侧为事件处理函数名称,如果为空白,表示还没有事件处理函数,选中Click事件,双击右侧空白处,增加单击事件处理函数。


图2.4.2B

完成以上设计后,集成环境生成的Foem1.cs文件如下,底色为黑色的代码是新增代码。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace e2
{
/// 
/// Form1 的摘要说明。
/// 
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;//定义Button类引用变量
/// 
/// 必需的设计器变量。
/// 
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// 
/// 清理所有正在使用的资源。
/// 
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// 
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// 
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();//生成对象
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 56);//修改属性
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 32);
this.button1.TabIndex = 0;
this.button1.Text = "确定";
this.button1.Click += new System.EventHandler(this.button1_Click);//增加事件
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
/// 
/// 应用程序的主入口点。
/// 
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{//事件处理函数
}
}
}

请注意按钮放到窗体后,集成环境自动增加的语句。分析这些增加的语句,可知在窗体中增加Button类对象的步骤:首先定义Button类变量button1,这是Form1类的一个字段,由于主窗体关闭,程序也就结束了,因此定义在主窗体Form1中的变量的生命周期和程序的生命周期是相同的,从这个意义上说,这样的变量是全局变量。因此变量button1和主窗体Form1有相同的生命周期。第二步在构造函数中用new生成Button类对象,第三步在构造函数中修改button1的属性,第四步增加button1的事件函数,函数button1_Click()是事件处理函数,语句this.button1.Click += new System.EventHandler(this.button1_Click)把按钮Button1的事件Click和事件处理函数button1_Click()联系到一起。程序员应在事件处理函数button1_Click()中增加具体的事件处理语句。这些步骤对于增加任何控件都是相同的。可以比较一下2.4.1节中的步骤,它们基本是相同的。应熟悉以上操作步骤,学会在窗体中增加控件,修改控件属性,增加事件函数。