//取模 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 = \ }