第1页共14页实验报告学院(系)名称:计算机科学与工程学院姓名**学号********专业计算机科学与技术班级2015级班实验项目实验二:Windows窗体程序开发课程名称.NET程序设计课程代码0667066实验时间2017年4月25日实验地点7-215批改意见成绩教师签字:一、实验目的(1)掌握如何创建Windows窗体应用程序并熟悉程序的结构;(2)掌握常用控件的使用;掌握控件的常用属性;(3)理解事件的含义,并掌握控件的常用事件的含义及应用;(4)掌握不同窗体之间传递数据的方式;(5)COM组件的应用。二、实验环境开发环境:PC机,Windows7操作系统;开发工具:VisualStudio2010以上。三、实验要求(1)认真完成实验内容,编写程序代码;(2)输出实验的正确结果;(3)书写并提交实验报告。四、实验内容1、文本框控件使用利用文本框控件的属性及事件,按如下图窗体所示完成:当输入单价和数量时能够自动正确计算出商品价值。代码实现:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceWindowsFormsApplication1第2页共14页{publicpartialclassForm1:Form{doublex=0;inty=0;publicForm1(){InitializeComponent();}privatevoidlabel2_Click(objectsender,EventArgse){}privatevoidtextBox1_TextChanged(objectsender,EventArgse){if(textBox1.Text!=){try{x=Convert.ToDouble(textBox1.Text);}catch{MessageBox.Show(请输入数字);}if(textBox2.Text!=){textBox3.Text=(x*y).ToString();}}}privatevoidtextBox2_TextChanged(objectsender,EventArgse){if(textBox2.Text!=){try{y=Convert.ToInt32(textBox2.Text);}catch第3页共14页{MessageBox.Show(请输入数字);}textBox3.Text=(x*y).ToString();}}privatevoidForm1_Load(objectsender,EventArgse){}}}运行结果:2、Timer组件的使用,要求利用Timer组件实现一时钟。代码实现:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;第4页共14页usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace_2._2{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){timer1.Tick+=newEventHandler(timer1_Tick);timer1.Interval=1000;timer1.Start();}privatevoidtimer1_Tick(objectsender,EventArgse){textBox1.Text=DateTime.Now.ToString(yyyy-MM-ddhh:mm:ss);}}}运行结果:3、ComboBox控件的使用已知数组data中存放政治面貌的值,试将data中的值初始化到ComboBox控件中,并实现当选中某个值时将其显示在一个TextBox控件中。代码实现:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;第5页共14页usingSystem.Text;usingSystem.Windows.Forms;namespace_2._3{publicpartialclassForm1:Form{string[]data={中共党员,共青团员,群众,民主党派,无党人士,其他};publicForm1(){InitializeComponent();}privatevoidlabel2_Click(objectsender,EventArgse){}privatevoidForm1_Load(objectsender,EventArgse){for(inti=0;idata.Length;i++){comboBox1.Items.Add(data[i]);}}privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse){textBox1.Text=comboBox1.Text;}}}运行结果:第6页共14页4、ListView控件的使用将二维数组中的信息在ListView控件中。二维数组用来模拟学生信息,具体信息:string[,]data={{20140001,张三,天津市西青区宾水西道391号},{20140002,李四,天津市西青区宾水西道391号}};试完成如下功能:(1)将data信息显示在ListView控件中;(2)当点击ListView控件中某个学生信息时,将相应的信息显示在对应的文本框控件中。代码实现:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace_2._4{publicpartialclassForm1:Form{string[]data1={1,20140001,张三,天津市西青区宾水西道391号};string[]data2={2,20140002,李四,天津市西青区宾水西道391号};publicForm1(){InitializeComponent();}第7页共14页privatevoidlistView1_SelectedIndexChanged(objectsender,EventArgse){}privatevoidForm1_Load(objectsender,EventArgse){listView1.View=View.Details;listView1.Columns.Add(序号,50,HorizontalAlignment.Center);listView1.Columns.Add(学号,80,HorizontalAlignment.Center);listView1.Columns.Add(姓名,80,HorizontalAlignment.Center);listView1.Columns.Add(地址,290,HorizontalAlignment.Center);ListViewItemlvi=newListViewItem(data1);listView1.Items.Add(lvi);lvi=newListViewItem(data2);listView1.Items.Add(lvi);}privatevoidlistView1_Click(objectsender,EventArgse){textBox1.Text=listView1.SelectedItems[0].SubItems[1].Text;textBox2.Text=listView1.SelectedItems[0].SubItems[2].Text;textBox3.Text=listView1.SelectedItems[0].SubItems[3].Text;}}}运行结果:第8页共14页5、控件综合应用(1)创建一个登录窗体界面,当用户输入:admin/admin时登录成功进入主界面。当输入的用户名和密码不等于admin/admin时应弹出提示对话框。(2)主界面设计与实现,要求:1.主界面设置为MDI窗体;2.主界面应具有菜单、工具栏及状态栏;3.将登录窗口输入的用户名显示在状态栏中;4.主界面中提供退出应用程序的功能;5.点击某个菜单或工具栏时,能够显示出对应的窗体,并作为主窗体的MDI子窗体。代码实现:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespace_2._5{publicpartialclassMainForm:Form{publicstringusername;publicMainForm(){InitializeComponent();}privatevoidMainForm_Load(objectsender,EventArgse){toolStripStatusLabel1.Text=username;第9页共14页toolStripLabel1.Text=用户管理;}privatevoidtoolStripStatusLabel1_Click(objectsender,EventArgse){}privatevoidtoolStripButton1_Click(objectsender,EventArgse){}privatevoidtoolStripLabel1_Click(objectsender,EventArgse){}privatevoidtoolStripLabel1_Click_1(objectsender,EventArgse){}privatevoid退ª?出?ToolStripMenuItem_Click(objectsender,EventArgse){Application.Exit();}privatevoidtoolStripMenuItem2_Click(objectsender,EventArgse){ziForm1zi=newziForm1();zi.MdiParent=this.Owner;MessageBox.Show(zi.IsMdiChild.ToString());zi.Show();}privatevoid系¦Ì统ª3管¨¹理¤¨ªSToolStripMenuItem_Click(objectsender,EventArgse){}第10页共14页privatevoidtoolStripMenuItem3_Click(objectsender,EventArgse){ziForm1zi=newziForm1();zi.MdiParent=this.Owner;zi.Show();}privatevoidtoo