实验六 windows编程(5)

2019-03-28 19:16

//取模 case 4:

c = a % b;

textBox3.Text = c.ToString(); //显示运算结果 break; } }

//输入第一个数字

private void textBox1_TextChanged(object sender, EventArgs e) {

if (textBox1.Text != \ {

if (double.TryParse(textBox1.Text, out a) == false) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \MessageBox.Show(\输入的不是数字,请重新输入!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);

textBox1.Focus(); return; } }

if (textBox2.Text != \ {

compute(); //调用计算方法 } }

//输入第二个数字

private void textBox2_TextChanged(object sender, EventArgs e) {

if (textBox2.Text != \ {

if (double.TryParse(textBox2.Text, out b) == false) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \MessageBox.Show(\输入的不是数字,请重新输入!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);

textBox2.Focus(); }

else if (textBox1.Text != \ {

int m = comboBox1.SelectedIndex; //获取当前选择的索引 //判断除法时除数是否为0 if (m == 3) {

if (b == 0) {

MessageBox.Show(\除数不能为0,请重新输入!\提示信息\MessageBoxIcon.Warning);

textBox2.Text = \ textBox2.Focus(); return; } }

//判断取模时模数是否为0 if (m == 4) {

if (b == 0) {

MessageBox.Show(\模数不能为0,请重新输入!\提示信息\MessageBoxIcon.Warning);

textBox2.Text = \ textBox2.Focus(); return; } }

compute(); //调用计算方法 } } }

private void Form1_Load(object sender, EventArgs e) { } } }

6. 试编写Windows应用程序,完成下列要求:

(1)Form1窗体设计界面如下:

(2)运算类型的下列列表中包括:加法、减法、乘法、除法、取模共5种操作;初始状态下,选择“加法”运算,当用户更改运算类型时,下面式子中的加号“+”应自动更改为相应的运算符;

(3)当用户在前两个文本框中输入时,最后得到结果的文本框始终是空白状态,注意该文本框是只读的,用户不能更改其值;只有当用户单击确定按钮时,结果文本框中才会显示正确的计算结果;

(4)使用过程中,用户修改运算类型时,三个文本框的内容自动清空;

(5)注意第一个和第二个文本框如果输入的不是数字时,要有异常处理检查,并自动清空里面的内容然后继续输入。

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms;

namespace Test6 {

public partial class Form1 : Form {

double a, b, c; public Form1() {

InitializeComponent(); }

//输入第一个数字

private void textBox1_TextChanged(object sender, EventArgs e) {

if (textBox1.Text != \ {

if (double.TryParse(textBox1.Text, out a) == false) {

textBox1.Text = \

MessageBox.Show(\输入的不是数字,请重新输入!\提示信息\

textBox1.Focus(); return; } } }

//输入第二个数字

private void textBox2_TextChanged(object sender, EventArgs e)

{

if (textBox2.Text != \ {

if (double.TryParse(textBox2.Text, out b) == false) {

textBox2.Text = \

MessageBox.Show(\输入的不是数字,请重新输入!\提示信息\

textBox2.Focus(); return; }

if (radioButton4.Checked) {

if (b == 0) {

MessageBox.Show(\除数不能为0,请重新输入!\\提示信息\

textBox2.Text = \ textBox2.Focus(); return; } }

if (radioButton5.Checked) {

if (b == 0) {

MessageBox.Show(\模数不能为0,请重新输入!\\提示信息\

textBox2.Text = \ textBox2.Focus(); return; } } } }

//确定按钮

private void button1_Click(object sender, EventArgs e) {

if (radioButton1.Checked) {

c = a + b;

textBox3.Text = c.ToString(); }

else if (radioButton2.Checked)

{

c = a - b;

textBox3.Text = c.ToString(); }

else if (radioButton3.Checked) {

c = a * b;

textBox3.Text = c.ToString(); }

else if (radioButton4.Checked) {

c = a / b;

textBox3.Text = c.ToString(); }

else if (radioButton5.Checked) {

c = a % b;

textBox3.Text = c.ToString(); } }

//显示加法运算符

private void radioButton1_MouseClick(object sender, MouseEventArgs e) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \ label3.Text = \ }

//显示减法运算符

private void radioButton2_CheckedChanged(object sender, EventArgs e) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \ label3.Text = \ }

//显示乘法运算符

private void radioButton3_CheckedChanged(object sender, EventArgs e) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \ label3.Text = \ }


实验六 windows编程(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:马克思主义基本原理概论期末模拟试卷及答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: