看了杨老师的WinForm基础视频里面的简单的计算器之后就很想试着尝试做一个跟电脑里一样的计算器,本人能力有限只能做成这样
按钮输入之后显示在文本框中,测试代码如下,通过这个练习让我更能熟练的视频里面讲解的一些小的知识点并且贯通起来
public Form1()
{
InitializeComponent();
this.textBox1.AppendText("\n");
{
InitializeComponent();
this.textBox1.AppendText("\n");
}
/// <summary>
/// 判断textBox显示文本方法
/// </summary>
/// <param name="nuber"></param>
private void step(String nuber) {
/// 判断textBox显示文本方法
/// </summary>
/// <param name="nuber"></param>
private void step(String nuber) {
this.textBox1.AppendText(nuber);
}
/// <summary>
/// 判断运算符出现的方法
/// </summary>
/// <param name="atr"></param>
private void Operator(String atr)
{
//判断如果输入运算符时textBox里面没有数字则在textBox上面返回值
if (this.textBox1.Text.Trim().IndexOf(atr) != 0 && this.textBox1.Text.Trim() != null && this.textBox1.Text.Trim() != "")
{
}
/// <summary>
/// 判断运算符出现的方法
/// </summary>
/// <param name="atr"></param>
private void Operator(String atr)
{
//判断如果输入运算符时textBox里面没有数字则在textBox上面返回值
if (this.textBox1.Text.Trim().IndexOf(atr) != 0 && this.textBox1.Text.Trim() != null && this.textBox1.Text.Trim() != "")
{
this.textBox1.Text = this.textBox1.Text.Trim() + atr;
}
else
{
return;
}
}
/// <summary>
/// 数字0-9按钮显示方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
step("1");
}
private void button2_Click(object sender, EventArgs e)
{
step("2");
}
}
else
{
return;
}
}
/// <summary>
/// 数字0-9按钮显示方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
step("1");
}
private void button2_Click(object sender, EventArgs e)
{
step("2");
}
private void button3_Click(object sender, EventArgs e)
{
step("3");
}
{
step("3");
}
private void button4_Click(object sender, EventArgs e)
{
step("4");
}
{
step("4");
}
private void button5_Click(object sender, EventArgs e)
{
step("5");
}
{
step("5");
}
private void button6_Click(object sender, EventArgs e)
{
step("6");
}
{
step("6");
}
private void button7_Click(object sender, EventArgs e)
{
step("7");
}
{
step("7");
}
private void button8_Click(object sender, EventArgs e)
{
step("8");
}
{
step("8");
}
private void button9_Click(object sender, EventArgs e)
{
step("9");
}
{
step("9");
}
private void button14_Click(object sender, EventArgs e)
{
step("0");
}
//清空textBox
private void button16_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
}
//显示加号方法
private void button10_Click(object sender, EventArgs e)
{
Operator("+");
}
//显示减号方法
private void button11_Click(object sender, EventArgs e)
{
Operator("-");
}
//显示乘号方法
private void button12_Click(object sender, EventArgs e)
{
Operator("*");
}
{
step("0");
}
//清空textBox
private void button16_Click(object sender, EventArgs e)
{
this.textBox1.Text = "";
}
//显示加号方法
private void button10_Click(object sender, EventArgs e)
{
Operator("+");
}
//显示减号方法
private void button11_Click(object sender, EventArgs e)
{
Operator("-");
}
//显示乘号方法
private void button12_Click(object sender, EventArgs e)
{
Operator("*");
}
//显示除号方法
private void button13_Click(object sender, EventArgs e)
{
Operator("/");
}
private void button13_Click(object sender, EventArgs e)
{
Operator("/");
}
/// <summary>
/// 等号控件方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button15_Click(object sender, EventArgs e)
{
/// 等号控件方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button15_Click(object sender, EventArgs e)
{
int i = 0;
int j = 0;
int men = 0;
String[] err = this.textBox1.Lines;
foreach (String itmes in err)
{
if (this.textBox1.Text.Trim().IndexOf("+") > -1)
{
String[] Er = itmes.Split('+');
i = Convert.ToInt32(Er[0]);
j = Convert.ToInt32(Er[1]);
men = i + j;
}
else if (this.textBox1.Text.Trim().IndexOf("-") > -1)
{
String[] Er = itmes.Split('-');
i = Convert.ToInt32(Er[0]);
j = Convert.ToInt32(Er[1]);
men = i - j;
}
else if (this.textBox1.Text.Trim().IndexOf("*") > -1)
{
int j = 0;
int men = 0;
String[] err = this.textBox1.Lines;
foreach (String itmes in err)
{
if (this.textBox1.Text.Trim().IndexOf("+") > -1)
{
String[] Er = itmes.Split('+');
i = Convert.ToInt32(Er[0]);
j = Convert.ToInt32(Er[1]);
men = i + j;
}
else if (this.textBox1.Text.Trim().IndexOf("-") > -1)
{
String[] Er = itmes.Split('-');
i = Convert.ToInt32(Er[0]);
j = Convert.ToInt32(Er[1]);
men = i - j;
}
else if (this.textBox1.Text.Trim().IndexOf("*") > -1)
{
String[] Er = itmes.Split('*');
i = Convert.ToInt32(Er[0]);
j = Convert.ToInt32(Er[1]);
men = i * j;
i = Convert.ToInt32(Er[0]);
j = Convert.ToInt32(Er[1]);
men = i * j;
}
else if (this.textBox1.Text.Trim().IndexOf("/") > -1)
{
else if (this.textBox1.Text.Trim().IndexOf("/") > -1)
{
String[] Er = itmes.Split('/');
i = Convert.ToInt32(Er[0]);
j = Convert.ToInt32(Er[1]);
if (j == 0)
{
MessageBox.Show("除数不能为零");
}
men = i / j;
}
this.textBox1.Text = Convert.ToString(men);
}
i = Convert.ToInt32(Er[0]);
j = Convert.ToInt32(Er[1]);
if (j == 0)
{
MessageBox.Show("除数不能为零");
}
men = i / j;
}
this.textBox1.Text = Convert.ToString(men);
}
}
---------------------- Windows Phone 7手机开发、 .Net培训、期待与您交流! ----------------------
详细请查看: http://net.itheima.com/