第章Windows窗体应用程序开发.ppt

上传人:本田雅阁 文档编号:3139719 上传时间:2019-07-16 格式:PPT 页数:95 大小:4.10MB
返回 下载 相关 举报
第章Windows窗体应用程序开发.ppt_第1页
第1页 / 共95页
第章Windows窗体应用程序开发.ppt_第2页
第2页 / 共95页
第章Windows窗体应用程序开发.ppt_第3页
第3页 / 共95页
第章Windows窗体应用程序开发.ppt_第4页
第4页 / 共95页
第章Windows窗体应用程序开发.ppt_第5页
第5页 / 共95页
点击查看更多>>
资源描述

《第章Windows窗体应用程序开发.ppt》由会员分享,可在线阅读,更多相关《第章Windows窗体应用程序开发.ppt(95页珍藏版)》请在三一文库上搜索。

1、第9章 Windows窗体应用程序开发,本 章 要 点,Windows窗体开发基础 常用Windows控件 Windows菜单设计 工具栏与状态栏设计 通用对话框编程,9.1 Windows窗体编程,9.1.1 WinForms概述,可视化界面组件统称为控件,程序执行流程控制模式:,过程控制模式(非图形用户界面应用程序的传统控制模式) 事件驱动模式(Windows图形用户界面应用程序的控制模式),9.1.1 WinForms概述,“开始”“程序”“Microsoft Visual Studio.NET 2008”“Microsoft Visual Studio.NET 2008”,9.1.1

2、WinForms概述,9.1.1 WinForms概述,设计窗口,各种控件,属性,放置控件的区域,using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication3 public partial class Form1 : Form public Form1() Initial

3、izeComponent(); ,提供了大量绘图工具的访问权限,基础核心命名空间,ArrayList、BitArray、Hashtable、Stack、StringCollection 和 StringTable 类,大量窗体和控件,从 System.Windows.Forms.Form 派生,Visual Studio .NET 生成的代码,9.1.1 WinForms概述,partial class Form1 / / 必需的设计器变量。 / private System.ComponentModel.IContainer components = null;,构造函数调用 Initial

4、izeComponent() 方法,#region Windows 窗体设计器生成的代码 / / 设计器支持所需的方法 - 不要 / 使用代码编辑器修改此方法的内容。 / private void InitializeComponent() this.SuspendLayout(); / / Form1 / this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new

5、 System.Drawing.Size(333, 266); this.Name = “Form1“; this.Text = “Form1“; this.ResumeLayout(false); #endregion,项目的容器,9.1.1 WinForms概述,/ / 清理所有正在使用的资源。 / / 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) if (disposing ,释放系统资源,9.1.1 WinForms概述,static class Program / / 应用程序的

6、主入口点。 / STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(); ,程序的主入口点,9.1.1 WinForms概述,标签,按钮,组合框,列表框,文本框,9.1.1 WinForms概述,(1)创建窗体 (2)为窗体添加控件 (3)设计控件在窗体中的布局 (4)设置各控件属性 (5)编写代码,响应控件事件,GUI(Graphics User Interfa

7、ce),即图形用户界面,是指用图形的方式实现人与计算机之间的交互。,9.1.2 编写GUI应用程序的步骤,9.1.3 Control类,.NET中的大多数控件都派生于System.Windows.Forms.Control类。Control类实现了所有窗体交互控件的基本功能:处理用户键盘输入、处理消息驱动、限制控件大小等等。,Control类的属性,Control类的属性,Control类的属性,Name:,9.1.4 控件的基本属性,用来获取或设置控件的名称。名称是控件的标识。,Text:,用来获取或设置控件的标题文字。,Width和Height:,用来获取或设置控件的大小。,X和Y:,用来

8、获取或设置控件的位置。,Visible:,用来获取或设置控件是否可见。取值为true或false。,Enabled:,9.1.4 控件的基本属性,ForeColor:,用来获取或设置控件的前景色。,BackColor:,用来获取或设置控件的背景色。,Font:,用来获取或设置控件的字体。,BorderStyle:,用来获取或设置控件的边框。,用来获取或设置控件是否对响应交互。取值为true或false。,AutoSize:,9.1.4 控件的基本属性,Anchor:,Dock:,获取或设置控件停靠到父容器的哪一个边缘。,TextAlign:,用来确定文本对齐方式。,Cursor:,用来获取或设

9、置鼠标移到控件上时,被显示的鼠标指针类型。,用来获取或设置控件是否自动调整大小。取值为true或false。,获取或设置控件的哪些边缘锚定到其容器边缘。,(1)Name: 用来获取或设置控件的名称。名称是控件的标识。 (2)Text: 用来获取或设置控件的标题文字。 (3)Width和Height: 用来获取或设置控件的大小。 (4)X和Y: 用来获取或设置控件的位置。 (5)Visible: 用来获取或设置控件是否可见。取值为true或false。 (6)Enabled: 用来获取或设置控件是否对响应交互。取值为true或false。 (7)ForeColor: 用来获取或设置控件的前景色。

10、 (8)BackColor: 用来获取或设置控件的背景色。,(9)Font: 用来获取或设置控件的字体。 (10)BorderStyle: 用来获取或设置控件的边框。 (11)AutoSize: 用来获取或设置控件是否自动调整大小。取值为true或false。 (12)Anchor: 获取或设置控件的哪些边缘锚定到其容器边缘。 (13)Dock: 获取或设置控件停靠到父容器的哪一个边缘。 (14) TextAlign: 用来确定文本对齐方式。 (15)Cursor: 用来获取或设置鼠标移到控件上时,被显示的鼠标指针的类型。,在窗体上放置一个图片框、一个“显示”按钮和一个“隐藏”按钮。开始运行时

11、,图片不可见,“显示“按钮可用,“隐藏”按钮不可用。单击“显示”按钮后,图片显示出来,“显示”按钮变成不可用,而“隐藏”按钮变成可用。,例9-1:图片的显示和隐藏,问题描述,9.1.5 事件和事件处理,事件处理程序是绑定到事件的方法。当引发事件时,执行事件处理程序内的代码。每个事件处理程序提供两个处理事件的参数。如下为Button 控件的 Click 事件的事件处理程序:,第一个参数 sender 提供对引发事件的对象的引用。第二个参数e传递针对要处理的事件的对象。通过引用对象的属性(有时引用其方法)可获得一些信息,如鼠标事件中鼠标的位置或拖放事件中传输的数据。,private void bu

12、tton1_Clicked(object sender, System.EventArgs e) ,键盘事件: 当用户对键盘进行操作时,系统会产生相应的键盘事件。当用户按下某个键时,就会发生KeyDown事件,随后便发生KeyPress事件。而当用户释放某个键时,发生KeyUp事件。当用户按住某个键不放时,KeyDown和KeyPress事件会不停地重复触发。,9.1.5 事件和事件处理,鼠标事件: 鼠标是一种定位输入设备,通常有两个或三个按钮(左、中和右按钮),还可能有滚轮。它的基本操作方式主要有:单击、双击和滚动。,9.1.5 事件和事件处理,Control类的事件,Control类的方法

13、,Control类的方法,例9-2:图片显示控制,问题描述,窗体上用图片框显示一张图片。当在图片框上单击鼠标左键时图片放大10%,单击鼠标右键时图片缩小10%。鼠标在图片框上移动时,窗体标题显示当前位置坐标。,9.2 常用控件,标签(Label):用于显示用户不能编辑的文本或图象,起标注或说明作用。该控件不能获得焦点。,9.2.1 标签,标签,标签的属性、事件与方法,9.2.1 标签,按钮(Button):提供用户与应用程序的交互,单击按钮来执行相应的操作。,9.2.2 按钮,按钮,按钮的属性、事件与方法,9.2.2 按钮,9.2.2 按钮,Click事件练习。,private void bu

14、tton1_Click(object sender,System.EventArgs e) MessageBox.Show(this,“你点击了确定按钮。“, “提示信息“,MessageBoxButtons.OK, MessageBoxIcon.Information); ,9.2.2 按钮,MouseEnter事件练习。,private void button1_MouseEnter(object sender,System.EventArgs e) /颜色在System.Drawing名称空间下 this.button1.BackColor=Color.Red; ,9.2.2 按钮,Mo

15、useLeave事件练习。,private void button1_MouseLeave(object sender,System.EventArgs e) this.button1.BackColor=SystemColors.Control; ,文本框(TextBox):文本框经常用于获取用户输入或显示文本,通常用于可编辑文本,也可以设定其成为只读控件。文本框能够显示多行数据,并添加基本的格式设置。,9.2.3 文本框,文本框,Text属性是文本框最重要的属性,要显示的文本就包含在Text属性中。Text属性可以在设计窗口时使用属性窗口设置,也可以在运行时用代码设置或者通过用户输入设置,

16、同样也可以在运行时通过读取Text属性来获得文本框的当前内容。,文本框的属性、事件与方法,9.2.3 文本框,设计一个乘法运算窗口,如下图所示,该窗口能完成浮点数乘法运算。,例9-3:乘法运算,说明,private void button1_Click(object sender, EventArgs e)/求积 double a=0, b=0, s=0; try a = Convert.ToDouble(textBox1.Text); b = Convert.ToDouble(textBox2.Text); catch(Exception exp) MessageBox.Show(exp.M

17、essage); s = a * b; textBox3.Text = Convert.ToString(s); private void button2_Click(object sender, EventArgs e)/清空 textBox1.Clear(); textBox2.Clear(); textBox3.Clear(); private void button3_Click(object sender, EventArgs e)/退出 this.Close(); ,设计一个登录窗口。登录窗口是很多应用系统中不可缺少的组成部分。通过验证用户输入的用户名和密码,决定是否允许用户进入系

18、统,可在一定程度上保证系统的安全。,例9-4:登录窗体设计,说明,private void button1_Click(object sender, EventArgs e) if (this.textBox1.Text = “abc”) ,单选按钮(RadioButton):单选按钮(RadioButton)通常成组出现,用于为用户提供两个或多个互相排斥的选项。 单选按钮是以他们所在的容器划分组的,直接在Form上放置的单选按钮将自动成为一组,这时Form就是容器,当选中容器中的一个单选按钮时,其他的将自动撤销选中。,9.2.4 单选按钮控件,如果要在一个Form上创建多个单选按钮组,则需要

19、使用GroupBox或者Panel控件作为容器。 把单选按钮组合在一起,使它们创建一个逻辑单元。首先在窗体上拖放一个GroupBox控件(组框),再把需要的RadioButton按钮放在组框的边界内,RadioButton按钮知道如何改变自己的状态,以反应组框中唯一被选中的选项。,9.2.4 单选按钮控件,单选按钮的属性、事件,9.2.4 单选按钮控件,例9-5: RadioButton控件的实例练习,复选按钮( CheckBox ):复选框(CheckBox)指示某特定条件是打开的还是关闭的。当用户希望选择一个或多个选项时,就需要使用复选框。多个CheckBox控件构成一组选项时,可以进行多

20、重选择,相互之间不影响。,9.2.5 复选按钮控件,复选按钮的属性、事件,9.2.5 复选按钮控件,一个关于RadioButton控件以及CheckBox控件的使用的简单例子。程序界面如下:,例9-6:简单的学生信息调查的设计与实现,private void button1_Click(object sender, EventArgs e) string name; string sex; string course=null; name = textBox1.Text; if (radioButton1.Checked) sex = radioButton1.Text; else sex =

21、 radioButton2.Text; if (checkBox1.Checked) course += checkBox1.Text + “ “; if (checkBox2.Checked) course += checkBox2.Text + “ “; if (checkBox3.Checked) course += checkBox3.Text + “ “; if (checkBox4.Checked) course += checkBox4.Text + “ “; textBox2.Show(); textBox2.Text = “姓名:“ + name + “rnrn“ + “性别

22、:“ + sex + “rnrn“ + “所学过的课程:“ + course; ,本案例建立一个使用单选钮和复选钮的应用程序,界面上有一个文本框,两组单选钮和一组复选钮。两组单选钮分别用于设置文本框的字体和字号,一组复选钮用于设置文本框的字型。程序界面如下:,例9-7:字体设置案例,说明,/“字体宋体”设置 private void radioButton1_CheckedChanged(object sender, EventArgs e) textBox1.Font = new Font(“宋体“, textBox1.Font.Size,textBox1.Font.Style); /“字体

23、隶书”设置 private void radioButton2_CheckedChanged(object sender, EventArgs e) textBox1.Font = new Font(“隶书“, textBox1.Font.Size, textBox1.Font.Style); /“字体楷体_GB2312”设置 private void radioButton3_CheckedChanged(object sender, EventArgs e) textBox1.Font = new Font(“楷体_GB2312“, textBox1.Font.Size, textBox1

24、.Font.Style); /“字号小五”设置 private void radioButton6_CheckedChanged(object sender, EventArgs e) textBox1.Font = new Font(textBox1.Font.Name, 9f, textBox1.Font.Style); /“字号三号”设置 private void radioButton5_CheckedChanged(object sender, EventArgs e) textBox1.Font = new Font(textBox1.Font.Name, 15.75f, text

25、Box1.Font.Style); /“字号一号”设置 private void radioButton4_CheckedChanged(object sender, EventArgs e) textBox1.Font = new Font(textBox1.Font.Name, 26.25f, textBox1.Font.Style); ,/“字型加粗”设置 private void checkBox1_CheckedChanged(object sender, EventArgs e) if (checkBox1.Checked) textBox1.Font = new Font(tex

26、tBox1.Font.Name, textBox1.Font.Size, textBox1.Font.Style | FontStyle.Bold); else textBox1.Font = new Font(textBox1.Font.Name, textBox1.Font.Size, textBox1.Font.Style ,列表框( ListBox ):列表框控件用来显示一组条目,以便让操作者从中选择一条或者多条,然后进行相应的处理。,9.2.6 列表框,Items:,列表框的主要属性,MultiColumn:,ColumnWidth:,获取或设置多列ListBox 中列的宽度。,Se

27、lectedIndex:,获取或设置ListBox中当前选定项的从零开始的索引。,获取或设置ListBox 的项。,获取或设置一个值,该值指示 ListBox 是否支持多列。,SelectedItems:,获取包含ListBox中当前选定项的集合。,SelectionMode:,获取或设置在 ListBox 中选择项所用的方法。,Sorted:,获取或设置一个值,该值指示 ListBox 中的项是否按字母顺序排序。,SelectedItem:,获取或设置ListBox中的当前选定项。,列表框的主要属性,Items.Add(),Items.Clear(),从集合中移除所有的项。,Items.In

28、sert(),将项插入列表框的指定索引处。,Items.Remove(),从集合中移除指定的对象。,Items.RemoveAt(),移除集合中指定索引处的项。,向 ListBox 的项列表添加项。,列表框的主要方法,例9-8:列表框的使用举例,/添加新的课程 private void button1_Click(object sender, EventArgs e) bool exit = false; if (textBox1.Text = “) MessageBox.Show(“所添加的课程不能为空。“); else for (int i = 0; i listBox1.Items.Co

29、unt; i+) if (textBox1.Text = listBox1.Itemsi.ToString() MessageBox.Show(“不能添加重复的课程。“); exit = true; textBox1.Clear(); break; if (exit = false) listBox1.Items.Add(textBox1.Text); textBox1.Clear(); ,/”-”按钮单击事件 private void button2_Click(object sender, EventArgs e) try this.listBox2.Items.Add(this.list

30、Box1.SelectedItem); this.listBox1.Items.Remove(this.listBox1.SelectedItem); catch(Exception exp) MessageBox.Show(exp.Message); /”-”按钮单击事件 private void button3_Click(object sender, EventArgs e) try this.listBox1.Items.Add(this.listBox2.SelectedItem); this.listBox2.Items.Remove(this.listBox2.SelectedI

31、tem); catch(Exception exp) MessageBox.Show(exp.Message); ,/”按钮单击事件 private void button4_Click(object sender, EventArgs e) for(int i=0;ithis.listBox1.Items.Count;i+) this.listBox2.Items.Add(this.listBox1.Itemsi); this.listBox1.Items.Clear(); /”按钮单击事件 private void button5_Click(object sender, EventArg

32、s e) for (int i = 0; i this.listBox2.Items.Count; i+) this.listBox1.Items.Add(this.listBox2.Itemsi); this.listBox2.Items.Clear(); ,组合框( ComboBox ):组合框由两部分组成,即一个文本框和一个列表框。文本框可以用来显示当前选中的条目,如果文本框可以编辑,则可以直接输入选择的条目。单击文本框旁边带有向下箭头的按钮,则会弹出列表框,使用键盘或者鼠标可以在列表框中选择条目。,9.2.7 组合框,DropDownStyle:,组合框的主要属性,Items:,Max

33、DropDownItems :,下拉列表能显示的最大条目数(1100),如果实际条目数大于此数,将出现滚动条。,确定下拉列表组合框类型。,存储ComboBox中的列表内容 。,Simple:表示文本框可编辑,列表部分永远可见。 DropDown:是默认值,表示文本框可编辑,必须单击箭头 才能看到列表部分。 DropDownList:表示文本框不可编辑,必须单击箭头才能 看到列表部分。,SelectedItem:,SelectedIndex:,编辑框所选列表条目的索引号,列表条目索引号从0开始。如果编辑框未从列表中选择条目,该值为-1 。,所选择条目的内容,即下拉列表中选中的字符串。如一个也没选

34、,该值为空。其实,属性Text也是所选择的条目的内容。,组合框的主要属性,Sorted:,表示下拉列表框中条目是否以字母顺序排序,默认值为false,不允许。,组合框的主要事件,被选索引号改变时发生的事件。,SelectedIndexChanged:,本例是要在左边的ComboBox中选择一个俱乐部名称,在右边的ComboBox中自动添加一些被选中的俱乐部的球员名称,单击【确定】按钮,则弹出所选择的信息。 单击【取消】按钮,退成应用程序。,例9-9:球员俱乐部,问题描述,private void comboBox1_SelectedIndexChanged(object sender, Eve

35、ntArgs e) switch (boBox1.SelectedIndex) case 0: boBox2.Items.Clear(); boBox2.Items.Add(“皮尔洛“); boBox2.Items.Add(“卡卡“); boBox2.Items.Add(“西多夫“); boBox2.Items.Add(“马尔蒂尼“); boBox2.Items.Add(“吉拉蒂诺“); boBox2.Items.Add(“迪达“); break; case 1: boBox2.Items.Clear(); boBox2.Items.Add(“伊布拉希莫维奇“); boBox2.Items.A

36、dd(“阿德里亚诺“); boBox2.Items.Add(“塞萨尔“); boBox2.Items.Add(“坎比亚索“); boBox2.Items.Add(“克雷斯波“); boBox2.Items.Add(“斯坦科维奇“); break;,case 2: boBox2.Items.Clear(); boBox2.Items.Add(“卡莫拉内西“); boBox2.Items.Add(“蒂亚格“); boBox2.Items.Add(“布冯“); boBox2.Items.Add(“特雷泽盖“); boBox2.Items.Add(“皮耶罗“); boBox2.Items.Add(“内

37、德维德“); break; case 3: boBox2.Items.Clear(); boBox2.Items.Add(“托蒂“); boBox2.Items.Add(“塔岱依“); boBox2.Items.Add(“小曼奇尼“); boBox2.Items.Add(“皮萨罗“); boBox2.Items.Add(“梅克斯“); boBox2.Items.Add(“帕努奇“); break; default: break; ,private void button2_Click(object sender, EventArgs e) this.Close(); Application.E

38、xit(); private void button1_Click(object sender, EventArgs e) string club = boBox1.IboBox1.SelectedIndex.ToString(); string player = boBox2.IboBox2.SelectedIndex.ToString(); MessageBox.Show(“俱乐部:“+club+“rn“+“球员:“+player,“球员介绍“,MessageBoxButtons.OKCancel,MessageBoxIcon.Information); ,本案例建立一个模拟在线购物程序,

39、用户输入相应的信息后单击“确定”按钮,显示出用户提交的有关信息。,例9-10:在线购物。,说明,/“商品类型”组合框选项改变 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) switch (comboBox1.SelectedIndex) case 0: listBox1.Items.Clear(); listBox1.Items.Add(“显示器“); listBox1.Items.Add(“光驱“); listBox1.Items.Add(“打印机“); listBox1.Items.Add(“

40、键盘“); break; case 1: listBox1.Items.Clear(); listBox1.Items.Add(“计算机应用基础“); listBox1.Items.Add(“Java程序设计“); listBox1.Items.Add(“操作系统“); listBox1.Items.Add(“Visual C#.NET“); break; ,/“”按钮单击事件 private void button1_Click(object sender, EventArgs e) bool exit=false; for (int i = 0; i listBox2.Items.Coun

41、t; i+) if (listBox2.Itemsi.ToString() = listBox1.SelectedItem.ToString() MessageBox.Show(“该产品已经选择“); exit = true; break; if (exit = false) listBox2.Items.Add(listBox1.SelectedItem.ToString(); /“”按钮单击事件 private void button2_Click(object sender, EventArgs e) listBox2.Items.Remove(listBox2.SelectedItem

42、.ToString(); ,/“”按钮单击事件 private void button3_Click(object sender, EventArgs e) for (int i = 0; i listBox1.Items.Count; i+) bool exit = false; for(int j=0;jlistBox2.Items.Count;j+) if (listBox1.Itemsi.ToString() = listBox2.Itemsj.ToString() exit = true; break; if (exit = false) listBox2.Items.Add(lis

43、tBox1.Itemsi.ToString(); /“”按钮单击事件 private void button4_Click(object sender, EventArgs e) listBox2.Items.Clear(); ,/“确定”按钮单击事件 private void button5_Click(object sender, EventArgs e) string name = this.textBox1.Text; string phone = this.textBox2.Text; string comm = “; string payment = “; string ackno

44、wledgement = “; for (int i = 0; i listBox2.Items.Count; i+) comm +=listBox2.Itemsi.ToString() + “ “; if (radioButton1.Checked) payment = radioButton1.Text; else payment = radioButton2.Text; if (checkBox1.Checked) acknowledgement += checkBox1.Text + “ “; if (checkBox2.Checked) acknowledgement += chec

45、kBox2.Text + “ “; if (checkBox3.Checked) acknowledgement += checkBox3.Text + “ “; this.textBox3.Text = “姓名:“ + name + “rn“ + “电话:“ + phone + “rn“ + “已选择的商品:“ + comm + “rn“ + “付款方式:“ + payment + “rn“ + “确认方式:“ + acknowledgement; ,/“取消”按钮单击事件 private void button6_Click(object sender, EventArgs e) this

46、.textBox1.Clear(); this.textBox2.Clear(); boBox1.SelectedIndex = 0; this.listBox1.Items.Clear(); this.listBox2.Items.Clear(); this.checkBox1.Checked = false; this.checkBox2.Checked = false; this.checkBox3.Checked = false; this.textBox3.Clear(); ,ToolTip组件,其作用是当鼠标停留在某个与ToolTip组件建立关联的控件上一段时间后,自动显示该控件功

47、能的提示信息。 主要属性有: Active:指示该控件当前是否处于激活状态。 AutomaticDelay:设置经过多长时间显示提示信息,缺省值为500ms。 AutoPopDelay:设置鼠标指针停留多长时间后消失提示信息。,9.2.8 ToolTip控件,例如:向设计窗体拖放一个ToolTip控件,然后在构造函数中加入下列代码:,this.toolTip1.SetToolTip(boBox1,“选择参加的小组。“); this.toolTip1.SetToolTip(boBox2,“选择左边小组参加的项目。“); this.toolTip1.SetToolTip(this.listBox1,“各小组参加的项目列表。“);,运行,分别将鼠标在上述控件中停留一段时间,观察显示的提示信息。,PictureBox控件是用于显示图像的Windows图片框控件。,9.2.9 PictureBox控件,PictureBox控件的主要属性,Image:,在PictureBox中显示的图片

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1