实验二:窗体(共12页).doc
精选优质文档-倾情为你奉上实验二 创建Windows窗体应用程序一、 实验目的1. 熟悉常用的Windows窗体控件(属性,事件)。 2. 熟悉Windows窗体设计和创建Windows窗体应用程序。 3. 熟悉通用对话框使用。二、实验内容1. 创建Windows窗体个人简历应用程序请编写一个简单的个人简历程序,要求可以通过文本框输入姓名,通过单选按钮设置性别,通过下拉列表框选择文化程度,通过文本区域填写其他个人信息;通过文件对话框选择照片并显示;通过两个下拉列表框来关联选择籍贯。(主要通过对窗体和各控件相应的事件处理函数进行编程,包括窗体的Load、下拉框的SelectedIndexChanged、按钮的Click事件)。参考:(1)新建一个名为“PersonalResume”的基于Windows应用程序的项目。(2)将文件“Form1.cs”重命名为“PersonInfo.cs”。(3)设计窗体,方法是添加五个标签控件、一个文本框、两个按钮、两个单选按钮、一个图片显示框、一个下拉列表框和列表框。如表所示列出了每个控件的属性。控件类型Name属性Text文本说明Labellab_name姓名: Labellab_sex性别: Labellab_nativePlace籍贯: Labellab_photo照片: Labellab_otherInfo其他信息: TextBoxtbx_name RadioButtonrbn_male男性 RadioButtonrbn_female女性 ComboBoxcbx_province ListBoxlbx_city PictureBoxpbx_photo RichTextBoxrtbx_otherInfo Buttonbtn_browse浏 览 Buttonbtn_OK确 定 (4)在窗体的Load事件中编写下列代码。private void PersonInfo_Load(object sender, EventArgs e) cbx_province.Items.Add("浙江省"); cbx_province.Items.Add("江苏省"); cbx_province.SelectedIndex = 0; (5)在cbx_province的SelectedIndexChanged事件中编写下列代码。private void cbx_province_SelectedIndexChanged(object sender, EventArgs e) if (cbx_province.SelectedItem != null) string provinceStr = cbx_province.SelectedItem.ToString().Trim(); string zjCitys ="杭州市","宁波市","温州市","绍兴市","台州市","湖州市","金 华市","嘉兴市","衢州市" ,"丽水市","舟山市"string jsCitys = "常州市", "淮安市", "连云港市", "南京市", "南通市", "苏州市", "宿迁市", "泰州市", "无锡市", "徐州市", "盐城市" ,"扬州市","镇江市" lbx_city.Items.Clear(); /清空所有的Item项 switch (provinceStr) case "浙江省": lbx_city.Items.AddRange(zjCitys); /添加浙江省的城市 break; case "江苏省": lbx_city.Items.AddRange(jsCitys); /添加江苏省的城市 break; (6)在btn_browse的Click事件中编写下列代码。private void btn_browse_Click(object sender, EventArgs e) OpenFileDialog imageDialog = new OpenFileDialog(); imageDialog.Filter = "BMP(*.BMP)|*.BMP|JPEG(*.JPEG)|*.JPEG|JPG(*.JPG)|*.JPG|GIF (*.GIF)|*.GIF|PNG(*.png)|*.png" imageDialog.Title = "选择照片" if (imageDialog.ShowDialog() = DialogResult.OK) pbx_photo.ImageLocation = imageDialog.FileName; (7)在btn_OK的Click事件中编写下列代码。private void btn_OK_Click(object sender, EventArgs e) if (tbx_name.Text = "" && tbx_name.Text.Length < 1) MessageBox.Show("请填写姓名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; if (lbx_city.SelectedItem = null) MessageBox.Show("请选择籍贯!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return; MessageBox.Show("你的简历信息将会保存到数据库中,n以便招聘企业查询!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); 2. C#窗体应用程序列表框应用实例参考代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace _1 public partial class Form1 : Form public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) button2.Enabled = false; button3.Enabled = false; button4.Enabled = false; private void button1_Click(object sender, EventArgs e) /按钮“添加” if (textBox1.Text = "") MessageBox.Show("请输入要添加的内容!"); textBox1.Focus(); else listBox1.Items.Add(textBox1.Text); textBox1.Text = "" textBox1.Focus(); private void button2_Click(object sender, EventArgs e)/按钮“移除” listBox1.Items.Remove(listBox1.Text); listBox2.Items.Remove(listBox2.Text); listBox1.SelectedIndex = -1; listBox2.SelectedIndex = -1; /this.Enabled = false; /button3.Enabled = false; /button4.Enabled = false; private void listBox1_SelectedIndexChanged(object sender, EventArgs e) / if (listBox1.SelectedIndex > 0) / / button2.Enabled = true; / button3.Enabled = true; / private void listBox1_Click(object sender, EventArgs e) /if (listBox1.SelectedIndex > 0) / button2.Enabled = true; button3.Enabled = true; / private void button3_Click(object sender, EventArgs e)/按钮“” listBox2.Items.Add(listBox1.Text); listBox1.Items.Remove(listBox1.Text); listBox1.SelectedIndex = -1; button4.Enabled = true; private void button4_Click(object sender, EventArgs e)/按钮“” listBox1.Items.Add(listBox2.Text); listBox2.Items.Remove(listBox2.Text); 3. 创建Windows应用程序,添加控件设置界面,添加Student类,该类仅包含学号、姓名与性别字段和属性。在窗体类定义中声明学生类对象,通过文本框设置对象的值,通过标签框输出对象的值。程序运行如下:class student private string sno; private string sname; private string sex; public string Sno get return sno; set sno = value; public string Sname get return sname; set sname = value; public string Sex get return sex; set sex = value; private void button1_Click(object sender, EventArgs e) Student s1 = new Student(); s1.Sno = "" s1.Sname = "张三" s1.Sex = "男" textBox1.Text = s1.Sno; textBox2.Text = s1.Sname; textBox3.Text = s1.Sex; label4.Text = "对象值设置完毕!" private void button2_Click(object sender, EventArgs e) Student s1 = new Student(); s1.Sno = "" s1.Sname = "张三" s1.Sex = "男" label4.Text = "学号:" + s1.Sno + "姓名:" + s1.Sname + "性别:" + s1.Sex;三、实验报告:1. 实验各项内容实现的过程。2. 实验结果分析。专心-专注-专业