上机作业是使用C#实现计算器的四则运算,教学提示是用button控件,不过我用的是comboBox控件,目前只能实现两位数的加减乘除,且不能先使用控件选择加减乘除,得待在两个textBox输入了Text值后才能使用comboBox控件实现加减乘除
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace caculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
double i = 0.0;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboBox1.Text == "+")
{
i = double.Parse(textBox1.Text) + double.Parse(textBox2.Text);
textBox3.Text = Convert.ToString(i);
}
else if (this.comboBox1.Text == "-")
{
i = double.Parse(textBox1.Text) - double.Parse(textBox2.Text);
textBox3.Text = Convert.ToString(i);
}
else if (this.comboBox1.Text == "*")
{
i = double.Parse(textBox1.Text) * double.Parse(textBox2.Text);
textBox3.Text = Convert.ToString(i);
}
else if (this.comboBox1.Text == "/")
{
i = double.Parse(textBox1.Text) / double.Parse(textBox2.Text);
textBox3.Text = Convert.ToString(i);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}