这个计算器非常基础,所以我只使用两个值来计算。
我只计算两个值val1
和val2
。这些值可以是任何大小,包括小数点。
我有数字0-9,小数点,加号,减号,除数,乘法和等号的按钮。
我想要val1,val2和operator(ex. +,-,/,*)
我选择计算这些值,以便在点击listBox
按钮后显示在=
中。我做的每一个计算都希望它添加到上一个计算下面的listBox中。当listBox增长到一定的计算量(10-15ish)时,我希望从底部删除最旧的计算,并将最新的计算添加到列表的顶部。
public partial class Form1 : Form
{
// global variables
bool plus = false;
bool minus = false;
bool multiply = false;
bool divide = false;
bool equal = false;
public Form1()
{
InitializeComponent();
} // **MUST HAVE**
private void button0_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "0";
} // makes number click usable
private void CheckIfEqual()
{
if (equal) // checks if equal is true
{
textBox1.Text = ""; // clear textBox
equal = false; // tells program there is new calculation
}
}
private void button1_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "1";
} // makes number click usable
private void button2_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "2";
} // makes number click usable
private void button3_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "3";
} // makes number click usable
private void button4_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "4";
} // makes number click usable
private void button5_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "5";
} // makes number click usable
private void button6_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "6";
} // makes number click usable
private void button7_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "7";
} // makes number click usable
private void button8_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "8";
} // makes number click usable
private void button9_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "9";
} // makes number click usable
private void buttonDecimal_Click(object sender, EventArgs e)
{
CheckIfEqual();
if (textBox1.Text.Contains("."))
{
return;
} // if it contains a decimal execute
else
{
textBox1.Text = textBox1.Text + ".";
} // adds decimal
} // end decimal
private void buttonPlusMinus_Click(object sender, EventArgs e)
{
if (textBox1.Text.Contains("-"))
{
textBox1.Text = textBox1.Text.Remove(0, 1);
} // if it contains the - character remove it
else
{
textBox1.Text = "-" + textBox1.Text;
} //
} // adds/removes -
private void buttonPlus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
return;
} // if textbox is empty return
else
{
plus = true;
textBox1.Tag = textBox1.Text; // stores number
textBox1.Text = ""; // clears textbox
} // tells program we are adding
} // end plus
private void buttonMinus_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
return;
} // if textbox is empty return
else
{
minus = true;
textBox1.Tag = textBox1.Text; // stores number
textBox1.Text = ""; // clears textbox
} // tells program we are subtracting
}
private void buttonMultiply_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
return;
} // if textbox is empty return
else
{
multiply = true;
textBox1.Tag = textBox1.Text; // stores number
textBox1.Text = ""; // clears textbox
} // tells program we are multiplying
}
private void buttonDivide_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
return;
} // if textbox is empty return
else
{
divide = true;
textBox1.Tag = textBox1.Text; // stores number
textBox1.Text = ""; // clears textbox
} // tells program we are dividing
}
private void buttonEquals_Click(object sender, EventArgs e)
{
equal = true; // tells program it made a calculation
if (plus) // if true execute
{
decimal dec = Convert.ToDecimal(textBox1.Tag) + Convert.ToDecimal(textBox1.Text);
textBox1.Text = dec.ToString(); // display value once add is finished
plus = false;
} // end if plus
if (minus) // if true execute
{
decimal dec = Convert.ToDecimal(textBox1.Tag) - Convert.ToDecimal(textBox1.Text);
textBox1.Text = dec.ToString(); // display value once add is finished
minus = false;
} // end if minus
if (multiply) // if true execute
{
decimal dec = Convert.ToDecimal(textBox1.Tag) * Convert.ToDecimal(textBox1.Text);
textBox1.Text = dec.ToString(); // display value once add is finished
multiply = false;
} // end if multiply
try
{
if (divide) // if true execute
{
decimal dec = Convert.ToDecimal(textBox1.Tag) / Convert.ToDecimal(textBox1.Text);
textBox1.Text = dec.ToString(); // display value once add is finished
divide = false;
} // end if divide
} // try
catch (DivideByZeroException ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK);
} // catch
} // end buttonEquals
private void buttonClear_Click(object sender, EventArgs e)
{
plus = minus = multiply = divide = false; // makes all false regardless
textBox1.Text = ""; // clear
textBox1.Tag = ""; // clears
}
} // end partial class Form1
}// end namespace
答案 0 :(得分:2)
重构会很好......
例如,您可以对所有“数字按钮单击”事件使用相同的方法。
而不是
private void button1_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "1";
} // makes number click usable
...
private void button9_Click(object sender, EventArgs e)
{
CheckIfEqual();
textBox1.Text = textBox1.Text + "9";
} // makes number click usable
只使用一种方法。
button1.Click += NumericButtonClick;
...
button9.Click +=NumericButtonClick;
private void NumericButtonClick(object sender, EventArgs e) {
CheckIfEqual();
textBox1.Text = textBox1.Text + ((Button)sender).Text;
}
反正。
假设您有一个ListBox historyListBox;
然后只需添加方法
private void PopulateListBox(string left, string right, string operator) {
var newContent = string.Format("{0} {1} {2}", left, operator, right);
if (historyListBox.Items.Count == 15)
historyListBox.Items.RemoveAt(14);//
historyListBox.Items.Insert(0, newContent);
}
然后你可以改变“equal_click事件”(也可以重构,但这是另一个问题)
private void buttonEquals_Click(object sender, EventArgs e)
{
var left = textBox1.Tag;
var right = textBox1.Text;
var operator = string.Empty;
equal = true; // tells program it made a calculation
if (plus) // if true execute
{
operator = "+";
decimal dec = Convert.ToDecimal(left) + Convert.ToDecimal(right);
textBox1.Text = dec.ToString(); // display value once add is finished
plus = false;
} // end if plus
if (minus) // if true execute
{
operator = "-";
decimal dec = Convert.ToDecimal(left) - Convert.ToDecimal(right);
textBox1.Text = dec.ToString(); // display value once add is finished
minus = false;
} // end if minus
//...etc
PopulateListBox(left, right, operator);
}