计算器算出结果后,再按数字会加在结果后面!
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 计算器
{
public partial class Form1 : Form
{
double num1, num2, result; // 操作数及运算结果
bool decimalFlag = false; // 判断输入的是否为小数
string myOperator; // 操作类型
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
txtReadout.Text = "0.";
// 指定文本框的对齐方式为右对齐
txtReadout.TextAlign = HorizontalAlignment.Right;
}
public void NumClick(int myNumber)
{
if (decimalFlag) // 输入小数
{
txtReadout.Text = txtReadout.Text + myNumber;
}
else
{
if (txtReadout.Text == "0.") // 输入整数
{
// 输入的是整数的第一个数字
txtReadout.Text = myNumber + ".";
}
else
{
// 输入的不是整数的第一个数字
txtReadout.Text = txtReadout.Text.Substring
(0, txtReadout.Text.Length - 1) + myNumber + ".";
}
}
}
private void btnAmount_Click(object sender, EventArgs e)
{
num2 = double.Parse(txtReadout.Text);
switch (myOperator)
{
case "add":
result = num1 + num2;
txtReadout.Text = result.ToString();
break;
case "Sub":
result = num1 - num2;
txtReadout.Text = result.ToString();
break;
case "Mul":
result = num1 * num2;
txtReadout.Text = result.ToString();
break;
case "Div":
if (num2 == 0)
{
txtReadout.Text = "除数不能为“0”!";
}
else
{
result = num1 / num2;
txtReadout.Text = result.ToString();
}
break;
}
}
private void btnNum1_Click_1(object sender, EventArgs e)
{
NumClick(1);
}
private void btnNum0_Click_1(object sender, EventArgs e)
{
NumClick(0);
}
private void btnNum3_Click(object sender, EventArgs e)
{
NumClick(3);
}
private void btnNum4_Click(object sender, EventArgs e)
{
NumClick(4);
}
private void btnNum2_Click(object sender, EventArgs e)
{
NumClick(2);
}
private void btnNum5_Click(object sender, EventArgs e)
{
NumClick(5);
}
private void btnNum6_Click(object sender, EventArgs e)
{
NumClick(6);
}
private void btnNum7_Click(object sender, EventArgs e)
{
NumClick(7);
}
private void btnNum8_Click(object sender, EventArgs e)
{
NumClick(8);
}
private void btnNum9_Click(object sender, EventArgs e)
{
NumClick(9);
}
private void btnAdd_Click(object sender, EventArgs e)
{
myOperator = "add";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
private void btnSub_Click(object sender, EventArgs e)
{
myOperator = "Sub";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
private void btnMul_Click(object sender, EventArgs e)
{
myOperator = "Mul";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
private void btnDiv_Click(object sender, EventArgs e)
{
myOperator = "Div";
num1 = double.Parse(txtReadout.Text);
txtReadout.Text = "0.";
decimalFlag = false;
}
private void btnDot_Click(object sender, EventArgs e)
{
decimalFlag = true;
}
private void button18_Click(object sender, EventArgs e)
{
num1 = 0;
num2 = 0;
txtReadout.Text = "0.";
decimalFlag = false;
}
}
} 这是我的代码,结果是可以计算出来得。但是算出结果后,再按数字键的话,这个数字会继续接在上次结果后面。例如:10+23=33 再按45 会变成3345.。哪位朋友告诉我怎么改进?PS: 我想要设置“<——”退格键,代码是什么样的?