已阅读5页,还剩52页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第6章 Windows应用程序6.1 Windows应用程序的结构Windows应用程序的开发方法是使用Visual Studio.Net。典型的Windows应用程序通常包括窗体(Form)、控件(Controls)、和相应的事件(Events)。6.2 Windows窗体设计器使用方法如前所述,略。6.3 控件.Net中的大多数控件都派生于System.Windows.Forms.Control类。这个类定义了控件的基本功能,因此,控件中的许多属性和事件都相同。定制(用户)控件派生于System.Windows.Forms.UserControl类。6.3.1 属性所有的控件都有许多属性,用于处理控件的操作。可参阅MSDN库查看类的所有属性。Control类最常见的属性,见教材P224表9-1。1 Text属性2 Anchor属性3 Dock属性6.3.2 事件在用户进行某一项操作(如:单击一个按钮)时,会引发某个事件的发生,此时,就会调用我们预先编好的事件处理程序代码,实现我们对程序的控制。最常见的事件,见教材P228表9-3。6.3.3 Control类的方法可以调用Control类的方法来获得控件的一些信息,或者设置控件的属性值及行为状态。最常见的Control类的方法,见教材P226表9-2。6.4 常用控件及其应用6.4.1 Button控件Visual Studio.NET中有三种按钮:矩形按钮、单选按钮、复选框。1.按钮主要用来执行3类任务:(1)用某种状态关闭对话框(例如OK和Cancel);(2)给对话框上输入的数据执行操作(例如输入搜索条件后,单击“搜索”按钮)(3)打开另一个对话框或应用程序(例如Help按钮)2.按钮最常用的事件是Click(EventHandler委托,EventArgs事件参数):当用户单击控件时引发该事件。当用户双击设计器中的这个控件时,该事件为默认事件。3.按钮的常用属性是:Text:在按钮表面显示的文本。例:按钮的测试(见按钮的测试程序)其中,Image属性旗帜图标来自:C:Microsoft Visual Studio.NET 2003common7GraphicsiconsFlags6.4.2 Label和LinkLabel控件标签控件用途只有一个:显示标题或简短提示,给用户解释窗体的某些内容。Visual Studio.NET中有2个标签控件:(1)Label:是标准的Windows标签。通常不需要添加任何事件处理代码。(2)LinkLabel:派生于标准标签,但以Internet链接的方式显示(超链接)。如:linklabel若希望用户单击它,进入文本中显示的网页,就需要添加事件处理代码。6.4.3 TextBox控件1.文本框的主要用途是让用户输入文本,用户可以输入任何字符,也可以限制用户只输入数值。2.文本框最常用的事件是TextChanged(EventHandler委托,EventArgs事件参数):当文本框中的文本发生变化(用户添加或删除字符)时引发该事件。当用户双击设计器中的这个控件时,该事件为默认事件。3. 文本框的常用属性是:Text:在文本框中显示的文本, Multiline,PasswordChar,ReadOnly,ScrollBars等。通常,在窗体上对TextBox中文本的有效性要加以验证,以确保文本框中不输入无效的字符,或者只输入某个范围内的数值。这需要TextBox控件提供的有效性验证事件。例1:制作一个简单的计算器,能够实现基本的加、减、乘、除功能。(见计算器程序)using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace 计算器public class Form1 : System.Windows.Forms.Formprivate System.Windows.Forms.Label label1;private System.Windows.Forms.TextBox textBox1;private System.Windows.Forms.Button btnclear;private System.Windows.Forms.Button btn1;private System.Windows.Forms.Button btn2;private System.Windows.Forms.Button btnjs;private System.Windows.Forms.Button btntui;private System.Windows.Forms.Button btn0;private void btnjs_Click(object sender, System.EventArgs e)float r;/用于保存计算结果 string t=textBox1.Text;/t用于保存文本框中的算术表达式int space=t.IndexOf( );/搜索空格的位置string s1=t.Substring(0,space);/s1用于保存第一个运算数char op=Convert.ToChar(t.Substring(space+1,1);/op用于保存运算符string s2=t.Substring(space+3);/s2用于保存第二个运算数float arg1=Convert.ToSingle(s1);/将运算数从string转换为float float arg2=Convert.ToSingle(s2);switch(op)case +:r=arg1+arg2;break;case -:r=arg1-arg2;break;case *:r=arg1*arg2;break;case /:if (arg2=0)throw new ApplicationException();elser=arg1/arg2;break;default:throw new ApplicationException();/异常处理textBox1.Text=r.ToString();/将计算结果显示在文本框中private void btnclear_Click(object sender, System.EventArgs e)textBox1.Text=;private void btnchu_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+ +btnchu.Text+ ;private void btnche_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+ +btnche.Text+ ;private void btnjian_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+ +btnjian.Text+ ;private void btnjia_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+ +btnjia.Text+ ;private System.Windows.Forms.Button btnche;private System.Windows.Forms.Button btn9;private System.Windows.Forms.Button btn8;private System.Windows.Forms.Button btn7;private System.Windows.Forms.Button btnjian;private System.Windows.Forms.Button btn6;private System.Windows.Forms.Button btn5;private System.Windows.Forms.Button btn4;private System.Windows.Forms.Button btnjia;private System.Windows.Forms.Button btn3;private System.Windows.Forms.Button btnchu;/声明components引用,它是一个保存我们所添加组件的数组。由于程序中未使用组件,所以引用为空。private System.ComponentModel.Container components = null;public Form1()/ Windows 窗体设计器支持所必需的/创建窗体的构造函数。InitializeComponent()可以在窗体中创建组建和控件,并设置它们的属性。InitializeComponent();static void Main() Application.Run(new Form1();private void btn1_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+btn1.Text;private void btn2_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+btn2.Text;private void btn3_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+btn3.Text;private void btn4_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+btn4.Text;private void btn5_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+btn5.Text;private void btn6_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+btn6.Text;private void btn7_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+btn7.Text;private void btn8_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+btn8.Text;private void btn9_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+btn9.Text;private void btn0_Click(object sender, System.EventArgs e)textBox1.Text=textBox1.Text+btn0.Text;private void btntui_Click(object sender, System.EventArgs e)Application.Exit();private void textBox1_TextChanged(object sender, System.EventArgs e)例2:创建一个对话框,在该对话框中可以输入姓名、地址、职业、年龄。(见例BegVCSharpChapter13TestBoxTest程序)要求进行合法性验证:用户名、住址不能为空;年龄不能为空且必须是一个大于或等于0的数字;职业必须是”Programmer”或为空。using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace TestBoxTestpublic class Form1 : System.Windows.Forms.Formprivate System.Windows.Forms.Label lblName;private System.Windows.Forms.Label lblAddress;private System.Windows.Forms.Label lblOccupation;private System.Windows.Forms.Label lblAge;private System.Windows.Forms.Label lblOutput;private System.Windows.Forms.Button btnOK;private System.Windows.Forms.Button btnHelp;private System.Windows.Forms.TextBox txtName;private System.Windows.Forms.TextBox txtAddress;private System.Windows.Forms.TextBox txtOccupation;private System.Windows.Forms.TextBox txtAge;private System.Windows.Forms.TextBox txtOutput;private System.ComponentModel.Container components=null;public Form1()InitializeComponent();this.btnOK.Enabled = false;/ Values for testing if the data is validthis.txtAddress.Tag = false;this.txtAge.Tag = false;this.txtName.Tag = false;this.txtOccupation.Tag = false;/ Subscriptions to events(订阅事件)this.txtName.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);this.txtAddress.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);this.txtOccupation.Validating += new System.ComponentModel.CancelEventHandler(this.txtOccupation_Validating);this.txtAge.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtAge_KeyPress);/事件需要按下键的信息this.txtAge.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);this.txtName.TextChanged += new System.EventHandler(this.txtBox_TextChanged);/控件文本发生改变时,激发Change事件this.txtAddress.TextChanged += new System.EventHandler(this.txtBox_TextChanged);this.txtAge.TextChanged += new System.EventHandler(this.txtBox_TextChanged);this.txtOccupation.TextChanged += new System.EventHandler(this.txtBox_TextChanged);/ / Clean up any resources being used./ protected override void Dispose( bool disposing ) if( disposing ) if (components != null) components.Dispose(); base.Dispose( disposing ); #region Windows Form Designer generated codestatic void Main() Application.Run(new Form1();private void btnOK_Click(object sender, System.EventArgs e)/ No testing for invalid values are made, as that should/ not be neccessarystring output;/ Concatenate the text values of the four TextBoxesoutput = Name: + this.txtName.Text + rn;output += Address: + this.txtAddress.Text + rn;output += Occupation: + this.txtOccupation.Text + rn;output += Age: + this.txtAge.Text;/ Insert the new textthis.txtOutput.Text = output;private void btnHelp_Click(object sender, System.EventArgs e)/ Write a short descrption of each TextBox in the Output TextBoxstring output;output = Name = Your name + rn;output += Address = Your address + rn;output += Occupation = Only allowed value is Programmer or empty + rn;output += Age = Your age;/ Insert the new textthis.txtOutput.Text = output;/姓名和住址文本框的有效性验证private void txtBoxEmpty_Validating(object sender, System.ComponentModel.CancelEventArgs e)TextBox tb;tb = (TextBox)sender;/ If the text is empty we set the background color of the / TextBox to red to indicate a problem. We use the tag value/ of the control to indicate if the control contains valid/ information.if (tb.Text.Length = 0)tb.BackColor = Color.Red;tb.Tag = false;elsetb.BackColor = System.Drawing.SystemColors.Window;tb.Tag = true;/ Finally we call ValidateAll which will set the value of/ the OK button.ValidateAll();private void ValidateAll()/ Set the OK button to enabled if all the Tags are truethis.btnOK.Enabled = (bool)(this.txtAddress.Tag) &(bool)(this.txtAge.Tag) &(bool)(this.txtName.Tag) &(bool)(this.txtOccupation.Tag); /职业文本框的有效性验证private void txtOccupation_Validating(object sender, System.ComponentModel.CancelEventArgs e)/ Cast the sender object to a textboxTextBox tb = (TextBox)sender;/ Check if the values are correctif (tb.Text.CompareTo(Programmer) = 0 | tb.Text.Length = 0)tb.Tag = true;tb.BackColor = System.Drawing.SystemColors.Window;elsetb.Tag = false;tb.BackColor = Color.Red;/ Set the state of the OK buttonValidateAll(); /年龄文本框的有效性验证private void txtAge_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)/0-9之间的ASCII值是48-57,Handled属性表示是否已处理了KeyPress事件if (e.KeyChar 57) & e.KeyChar != 8)/8是退格键e.Handled = true; / Remove the character private void txtBox_TextChanged(object sender, System.EventArgs e)/ Cast the sender object to a TextboxTextBox tb = (TextBox)sender;/ Test if the data is valid and set the tag background/ color accordingly.if (tb.Text.Length = 0 & tb != txtOccupation)tb.Tag = false;tb.BackColor = Color.Red;else if (tb = txtOccupation &(tb.Text.Length != 0 & tb.Text.CompareTo(Programmer) != 0)/ Dont set the color here, as it will color change while the user is typingtb.Tag = false;elsetb.Tag = true;tb.BackColor = SystemColors.Window;/ Call ValidateAll to set the OK buttonValidateAll();private void txtName_TextChanged(object sender, System.EventArgs e)private void txtOutput_TextChanged(object sender, System.EventArgs e)private void txtAddress_TextChanged(object sender, System.EventArgs e)private void txtOccupation_TextChanged(object sender, System.EventArgs e)private void txtAge_TextChanged(object sender, System.EventArgs e)private void Form1_Load(object sender, System.EventArgs e)例3生成随机整数(见随机数程序)static void Main() Application.Run(new Form1();private void btn1_Click(object sender, System.EventArgs e)Random randomNumber=new Random ();label1.Text=;for (int counter=1;counter=20;counter+)/循环20次int nextValue=randomNumber.Next(1,7);/产生1-6(包括6)之间的随机整数label1.Text+=nextValue.ToString()+ ;/将每个随机数添加到标签文本中if (counter%5=0)/每5个数后添加一个换行符label1.Text+=n;private void btn2_Click(object sender, System.EventArgs e)Application.Exit();本例题的说明:随机数可以用Random类(在命名空间System中)来引入到应用程序中。使用方法是:Random randomObject=new Random ();int randomNumber = randomObject.Next();Next方法产生一个从0到常量Int32.MaxValue(值为2147483647)的确定的整数值。由Next方法产生的数值事实上都是伪随机数由一个复杂的算术计算产生的序列。在该算术计算中,要有一个种子值。用某一个种子值总会产生同一个随机数序列。当我们创建随机数对象时,使用当前时间作为种子。因为每秒钟都在变化,所以程序每次执行时都会产生不同的随机数序列。Next方法的单参数形式产生一个范围从0到该参数值(但不包括该值)的随机序列。如:int value=randomObject.Next(6);将产生一个从0-5的随机整数序列。Next方法的双参数形式允许我们移位并且可以改变数字范围的缩小比例。如:int value=randomObject.Next(1,7);本例中我们对数字进行了移位,并产生了一个范围从1到7(但不包括7)的随机整数序列。生成随机小数可参阅NextDouble()方法(生成一个0.0-1.0之间的随机数)。例4计算从2100之间的所有偶数的和,把结果显示在消息框(MessageBox)中。(见程序msgBOX)static void Main() Application.Run(new Form1();private void button1_Click(object sender, System.EventArgs e)int sum=0;for(int number=2;number=100;number+=2)sum+=number;MessageBox.Show(The sum is +sum,Sum Even Integers from 2 to 100 ,MessageBoxButtons.OK ,MessageBoxIcon.Information );/*或使用MessageBox.Show(The sum is +sum,2-100偶数和 ,MessageBoxButtons.YesNo ,MessageBoxIcon.Question );*/private void button2_Click(object sender, System.EventArgs e)Application.Exit ();本例说明:(1)MessageBox.Show方法带4个参数:第一个参数是要显示的消息,第二个参数是在对话框的标题栏上显示的字符串,第三个参数是表明要显示哪个按钮的值,第四个参数是指明在消息的左边要显示哪个图标。(2)消息框(MessageBox)图标和按钮:(C#大学教程P98)图标:MessageBoxIcon.Information:专用的对话框 i,包含给用户的信息。 MessageBoxIcon.Exclamation:专用的惊叹号图标 !MessageBoxIcon.Question: 专用的问题标记图标 ? MessageBoxIcon.Error:专用的带有“X”的红色圆。按钮:MessageBoxButtons.OK MessageBoxButtons.OKCancel MessageBoxButtons.YesNo MessageBoxButtons.YesNoCancel MessageBoxButtons.RetryCancel MessageBoxButtons.AbortRetryIgnore 练习:1 编写一个“猜数”程序:任意输入一个1-1000之间的整数,然后,在标签上显示下列文本:“有一个位于1-1000之间的整数,你能猜中吗?请你输入猜的数:”提供一个用于输入数字的文本框,每一次输入数字,提供一个标签显示:“太大”或“太小”,帮助用户接近正确答案。同时,当用户输入错误答案时,文本框背景色为红色,当用户输入正确答案时,文本框背景色为绿色,并显示“正确!”。提供一个按钮可让用户重玩游戏。提供另一个按钮,可让用户退出游戏。2 编写一个程序:输入一个学生的学号、姓名和三门课程的成绩,计算并显示该生的总成绩及总平均成绩。提供一个按钮,可让用户退出程序。6.4.4 RadioButton(单选按钮)p3571单选按钮显示为一个标签,左边是一个圆点,该点可以是选中或未选中。用户提供几个互斥选项时,就可以使用单选按钮。例如:询问用户的性别。通常会有多个单选钮组合在一起,但在任何时候,按钮组中只能有一个单选钮被选中。2单选按钮的常用属性:(1)Checked:表示是否选中单选钮。如果控件中有一个选中标记,它就是true,否则为false。(2)Text: 单选按钮右侧显示的文本。3单选按钮的常用事件:(1)CheckedChanged: 单选按钮的选中改变时,引发这个事件。当用户双击设计器中这个控件时,该事件为默认事件。(2)Click:当用户单击控件时引发该事件。6.4.5 CheckBox(复选框)1复选框显示为一个标签,左边是一个小白方框,可以是选中或未选中。当选中复选框时,框中就会出现一个黑色的勾选标记;未选中时,复选框是空白。用户可以同时选择任意数量的复选框。2复选框的常用属性:(1)Checked:表示是否选中复选框。(2)Text: 复选框右侧显示的文本。(3)CheckState: 表示复选框是处于选中状态或者是处于未选中状态。3复选框的常用事件:(1) CheckedChanged: 每次选中或取消选中复选框时,引发这个事件。当用户双击设计器中这个控件时,该事件为默认事件。(2)CheckStateChanged:当CheckState属性发生变化时引发该事件。6.4.6 GroupBox(组框控件)该控件常和单选按钮和复选框控件一起使用 ,显示一个框架,其上有一个标题,其中可拖放一系列控件,组框控件是它们的父控件。例1:单选按钮和复选框的应用:(见单选复选按钮练习程序R)static void Main() Application.Run(new Form1();private void button1_Click(object sender, System.EventArgs e)string output;output = 职业: + (string)(this.chkP.Checked ? Programmer : Not a programmer) + rn;output += 性别: + (string)(this.rdof.Checked ? 男 : 女) + rn;this.textBox1.Text = output;private void button2_Click(object sender, System.EventArgs e)Application.Exit();例2:在6.4.3例2中,职业改为复选框;加入用户选择性别输入功能。(见BegVCSharpChapter13RadioButtonAndCheckBox程序)sing System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace TestBoxTestpublic class Form1 : System.Windows.Forms.Formprivate System.Windows.Forms.Label lblName;private System.Windows.Forms.Label lblAddress;private System.Windows.Forms.Label lblAge;private System.Windows.Forms.Label lblOutput;private System.Windows.Forms.Button btnOK;private System.Windows.Forms.Button btnHelp;private System.Windows.Forms.TextBox txtName;private System.Windows.Forms.TextBox txtAddress;private System.Windows.Forms.TextBox txtAge;private System.Windows.Forms.TextBox txtOutput;private System.ComponentModel.Container components;private System.Windows.Forms.GroupBox groupBox1;private System.Windows.Forms.CheckBox chkProgrammer;private System.Windows.Forms.RadioButton rdoMale;private System.Windows.Forms.RadioButton rdoFemale;public Form1()InitializeComponent();this.btnOK.Enabled = false;/ Tag values for testing if the data is validthis.txtAddress.Tag = false;this.txtAge.Tag = false;this.txtName.Tag = false;/ Subscriptions to eventsthis.txtName.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);this.txtAddress.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);this.txtAge.KeyPress += new System.Windows.Forms.KeyPressEve
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 代工免责协议书模板
- 广州市花都区人民法院2025年下半年公开招聘政府雇员工作易考易错模拟试题(共500题)试卷后附参考答案
- 医疗服务外包协议书
- 校园高利贷合同范本
- 广东惠州博罗县中小企业管理局招聘政府购买服务岗位工作人员易考易错模拟试题(共500题)试卷后附参考答案
- 公司提供宿舍协议书
- 企业消防联动协议书
- 山东日照市住房公积金管理中心2025年下半年招考工作人员易考易错模拟试题(共500题)试卷后附参考答案
- 宝鸡石油钢管限责任公司博士后科研工作站招考易考易错模拟试题(共500题)试卷后附参考答案
- 不锈钢安装合同范本
- 沪教牛津版七年级上册英语教材
- 【MOOC】民事诉讼法学-西南政法大学 中国大学慕课MOOC答案
- 《民航安全检查(安检技能实操)》课件-第三章 航空安全保卫法律、法规知识
- 通识写作:怎样进行学术表达学习通超星期末考试答案章节答案2024年
- 《全面质量管理》习题集(含答案)
- GB/T 44193-2024全国一体化政务服务平台一网通办基本要求
- 手术室竞选护士长
- 学校食堂冰箱清洗、除霜记录
- 叠加定理课件
- 公共政策导论全套教学课件
- 2024年青海电工考试题库电工高级工考试题库(全国通用)
评论
0/150
提交评论