文档详情

第5章Windows应用程序开发.ppt

发布:2017-06-17约4.57万字共115页下载文档
文本预览下载声明
第5章 Windows应用程序开发 Windows 窗体是用于 Microsoft Windows 应用程序开发基于.NET 框架的新平台。此框架提供一个有条理的、面向对象的、可扩展的类集,得以开发丰富的 Windows 应用程序。另外,Windows 窗体可作为多层分布式解决方案中的本地用户界面。 5.1 开发应用程序的步骤 利用Visual C#开发应用程序一般包括建立项目、界面设计、属性设计、代码设计等步骤。 1.建立项目 在Visual Studio.NET开发环境中选择“文件”菜单,选择“新建”选项中的“项目”,如图5.1所示。 1.建立项目 在“项目类型”列表框指定为“Visual C#项目”,在“模板”列表框里指定“Windows应用程序”作为模板,表示将以Visual C#作为程序设计语言,建立一个基于Windows界面的应用程序。同时在“位置”和“名称”列表框选择文件的保存位置和设定项目文件的名字。然后,单击“确定”,返回Visual Studio.NET的主界面,如图5.2所示。 5.1 开发应用程序的步骤 2.界面设计 在Visual Studio.NET的主界面,系统提供了一个缺省的窗体。可以通过工具箱向其中添加各种控件来设计应用程序的界面。具体操作是用鼠标按住工具箱需要添加的控件,然后拖放到窗体中即可。本例向窗体中分别添加2个button控件、3个label控件和3个textBox控件,调整各个控件的大小和位置如图5.3所示。 5.1 开发应用程序的步骤 3.设置属性 首先,在窗体中选中控件。然后在属性窗口设置该控件相应的属性。如表5.1所示。 5.1 开发应用程序的步骤 4.编写程序代码 进入开发环境的代码编辑器。编写代码如下: static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { long op1,op2,result; if ( (textBox1.Text == )|| (textBox2.Text==)) { MessageBox.Show(this,null,msg,MessageBoxButtons.OK , MessageBoxIcon.Information ); return; } 4.编写程序代码 try { op1=Convert.ToInt64(textBox1.Text ); op2=Convert.ToInt64(textBox2.Text ); result=op1+op2; textBox3.Text =Convert.ToString (result ); } catch(Exception el) { MessageBox.Show (this,el.Message ,msg,MessageBoxButtons.OK,Message BoxIcon.Warning ); } } private void button2_Click(object sender, System.EventArgs e) { this.Close(); } 4.编写程序代码 同时,系统将自动生成以下代码。 private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; 5.1 开发应用程序的步骤 6.保存项目 在“文件”菜单,单击“全部保存”或在工具
显示全部
相似文档