第05讲选择题应答器实例.pptVIP

  • 0
  • 0
  • 约2.4千字
  • 约 11页
  • 2019-09-22 发布于安徽
  • 举报
* 李德奇 主编 第五讲 选择题应答器实例 大连理工大学出版社 * 教学目标 掌握RadioButton控件的属性、事件和方法 掌握CheckBox控件的属性、事件和方法 掌握GroupBox控件的属性 掌握获取用户单选和多选结果的方法 完成选择题应答器实例的程序编写 * 教学内容 一、选择题应答器的功能及用户界面 二、选择题应答器的设计步骤 三、选择题应答器的主要程序代码 四、RadioButton控件 五、CheckBox控件 六、GroupBox控件 * 一、选择题应答器的功能及用户界面 在这个窗体中,用户可以对单选题和多选题作出选择。当单击【确定】按钮后,将在窗体右侧的文本框中显示答题的结果。单选题是由一组RadioButton控件表示和作答的,多选题是由一组CheckBox控件表示和作答的。程序运行的效果如下图。 * 二、选择题应答器的设计步骤 ①创建Windows应用程序项目,项目名称为“选择题应答器实例”。 ②向默认窗体中拖入3个Label控件,控件名称为label1—label3。这3个Label控件就是窗体中有深色背景的文字标签,用来标识题目和答案。 ③向窗体中拖入2个GroupBox控件,控件名称为groupBox1和groupBox2,其作用是将4个单选钮和4个检查框分成两组放置在其内。 ④向控件groupBox1中拖入4个RadioButton控件,控件名为radioButton1—radioButton4,用做单选题的选项。 ⑤向控件groupBox2中拖入4个CheckBox控件,控件名为checkBox1—checkBox4,用做多选题的选项。 ⑥向窗体中拖入1个TextBox控件,控件名为textBox1,用来显示答案。 ⑦向窗体中拖入1个Button控件,控件名为button1,用来启动提交答案的功能。 * 三、选择题应答器的主要程序代码(1/2) private void button1_Click(object sender, System.EventArgs e) { //构造单选题答案answer1 string answer1= .NET Framework是一种; if(radioButton1.Checked) answer1=answer1+radioButton1.Text+。; if(radioButton2.Checked) answer1=answer1+radioButton2.Text+。; if(radioButton3.Checked) answer1=answer1+radioButton3.Text+。; if(radioButton4.Checked) answer1=answer1+radioButton4.Text+。; * 三、选择题应答器的主要程序代码(2/2) //构造多选题答案answer2 string answer2=Button控件的事件有:; if(checkBox1.Checked) answer2=answer2+checkBox1.Text+、; if(this.checkBox2.Checked) answer2=answer2+checkBox2.Text+、; if(this.checkBox3.Checked) answer2=answer2+checkBox3.Text+、; if(this.checkBox4.Checked) answer2=answer2+checkBox4.Text+、; if(answer2.Length==13) { //若answer2的字符个数为13,一组CheckBox控件中未勾选任何选项 answer2=Button控件没有事件。; } else { int n=answer2.Length; //去掉answer2最后一个顿号,加上一个句号 answer2=answer2.Substring(0,n-1)+。; } //将两个答案合并显示在textBox1中 this.textBox1.Text=answer1+answer2; } * 四、RadioButton控件 RadioButton控件称为单选钮。当用户必须在多个选项中选择其一时,可以使用一组单选钮,此时必须用一个GroupBox控件将这些单选钮捆绑成组,使各单选钮间产生互斥效果。 1、常用的属性 Checked属性 设置和获取控件的选中状态。 AutoCheck属性 设置单选钮在单击时是否自动更改状态。 2、常用的事件 Click事件 单击控件时发生。 CheckedChange事件 Ch

文档评论(0)

1亿VIP精品文档

相关文档