当前位置:文档之家› 实验三:VS下基于控件的窗体应用程序设计解析

实验三:VS下基于控件的窗体应用程序设计解析

实验报告 成绩

课程名称 《.Net程序设计》 指导教师 实验日期 实验项目名称 实验三:VS下基于控件的窗体应用程序设计

一、 实验目的和要求 学习掌握windows程序设计的基本方法:①窗体设计使用,了解窗体对象的各项常用属性和方法。并通过这些属性,定制出不同的窗体对象。②掌握常用控件的特点、通过使用控件的常用属性来设置控件的外观及功能,熟悉并掌握利用控件事件编写代码的程序设计方式。学习C#下窗体的菜单栏、工具栏、状态栏的设计方法,完善窗体设计。掌握通过菜单控件实现窗体菜单的多级设计、掌握上下文菜单的设计方法。并通过菜单项的集合属性,深入了解集合的多层概念及使用。掌握工具栏的设计,了解其属性和方法,并通过属性让工具栏项目呈现出不同的外观。

二、 主要仪器设备、试剂或材料 微型计算机;Visual Studio 2010语言编译环境

三、 实验内容与要求, 1.创建窗体并修改窗体的各项属性,观察窗体的变化 2.在窗体中添加标签控件,按钮控件,编写程序,通过按钮控件的点击事件来改变标签的显示内容。 3.掌握文本框、日期和事件控件的使用,学习Timer控件的使用(参考课表P173) 4.学习并掌握窗体菜单、上下文菜单控件的设计方法,理解集合中的层次概念 5.学习并掌握工具栏控件的使用方法,设计不同外观的工具栏按钮。 6.掌握常见的对话框控件,编写程序使用打开/保存对话框,并将获取的信息通过标签显示出来。

四、 程序设计思路、运行及及结果分析 1.创建窗体并修改窗体的各项属性,观察窗体的变化 2.在窗体中添加标签控件,按钮控件,编写程序,通过按钮控件的点击事件来改变标签的显示内容。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;

namespace Samp10_3 { public partial class Form1 : Form { private Button button1 ; //Form1窗体类包含了一个按钮成员 public Form1() { InitializeComponent(); } private void InitializeComponent ( ) { this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Dock = System.Windows.Forms.DockStyle.Bottom; this.button1.Location = new System.Drawing.Point(0, 168); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(300, 32); this.button1.TabIndex = 0; this.button1.Text = "第一个按钮"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(300, 200); this.Controls.Add(this.button1); this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "使用代码向窗体添加一个按钮"; this.ResumeLayout(false); } static void Main()//主函数 { Application.Run (new Form1()) ; }

} } using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;

namespace Samp10_4 { public partial class Form1 : Form { private Button button1; //Form1窗体类包含了一个按钮成员 public Form1() { InitializeComponent(); //初始化窗体中的各个组件 } private void InitializeComponent() { //初始化窗体内各个组件 button1 = new Button(); //实例化一个按钮对象 SuspendLayout(); button1.Name = "button1"; button1.Size = new Size(117, 32); button1.Dock = DockStyle.Bottom; button1.Text = "第̨²一°?个?按ã¡ä钮£¤"; button1.Click += new System.EventHandler(button1_Click); //响应Click事件 this.AutoScaleBaseSize = new Size(6, 14); //设置窗体对象 this.ClientSize = new Size(300, 200); this.Controls.Add(button1); //将按钮对象添加到窗体中 this.Name = "Form1"; this.Text = "使用代码向窗体添加一个按钮"; this.StartPosition = FormStartPosition.CenterScreen; this.ResumeLayout(false); //响应鼠标移动事件 this.MouseMove += new System.Windows.Forms.MouseEventHandler(Form1_MouseMove); } static void Main() {//主函数 Application.Run(new Form1()); } //添加Click事件的响应代码? private void button1_Click(object sender, System.EventArgs e) { //编写响应|函数代码? //在此处添加具体响应代码? MessageBox.Show("Hello first button!"); } private void Form1_MouseMove(object sender, EventArgs e) { //鼠标移动事件处理代码? Point p = Cursor.Position; //定义一个点对象,用来获取当期鼠标所在点的坐标 //设置窗体的标题为当前鼠标的坐标 this.Text = "X:" + System.Convert.ToString(p.X) + " Y:" + System.Convert.ToString(p.Y); }

} } 3.掌握文本框、日期和事件控件的使用,学习Timer控件的使用(参考课表P173) 4.学习并掌握窗体菜单、上下文菜单控件的设计方法,理解集合中的层次概念 过程如下: (1) 打开Visual Studio 2010开发环境,创建基于C#的Windows应用程序; (2) 打开工具箱,定位到“菜单和工具” 栏,将“MenuStrip” 控件拖入默认窗体中;

(3)单击该控件,在文本框中输入“文件”,并按照提示依次输入“编辑”,“视图”,“数据”以及“格式”菜单项。 (4)在“文件”菜单项的下一级依次输入“新建”、 “打开”、 “关闭”以及“保存”,并在“新建”命令下一级输入“项目”与“网站”子命令。 (5) 添加快捷键 选中“打开”菜单项,在“属性”面板中设置ShortCutKeys属性,如下图所示: (6)添加图标。选中“打开”命令,在属性面板中设置Image属性,单击按钮,弹出选择资源对话框,单击导入按钮,选择本地适当图像资源,确认无误后单击确定按钮,完成Image属性设置。 (7)按Ctrl+F5组合键。 5.学习并掌握工具栏控件的使用方法,设计不同外观的工具栏按钮。 添加工具栏的步骤如下: 1.在窗体中添加一个工具栏控件 打开Visual Studio 2010开发环境,新建基于C#的Windows应用程序。打开工具箱,定位到“菜单和工具”栏,将“ToolStrip”控件拖入默认窗体Form1中

相关主题