本文介绍了清空后如何在texbox中添加第二个数字?计算器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Button[] numbers = new Button[10];
Button[] operators = new Button[5];
Button[] soperators = new Button[15];
string[] symbols = new string[]{ "+", "-", "*", "/" ,"="};
string[] scientific = new string[] {"sin","cos","tan","log","e","x2","^","!","(",")","[","]","Ans","rad","exp" };
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
if (i < 9)
{
numbers[i] = new Button();
numbers[i].Text = (i + 1).ToString();
numbers[i].Size = new Size(40, 40);
}
else
{
numbers[9] = new Button();
numbers[9].Text = "0";
numbers[9].Size = new Size(130, 40);
}
numbers[i].Click += btn_Click;
this.flowLayoutPanel2.Controls.Add(numbers[i]);
}
for (int i = 0; i < 5; i++)
{
operators[i] = new Button();
operators[i].Text = symbols[i];
operators[i].Click += op_Click;
this.flowLayoutPanel3.Controls.Add(operators[i]);
}
for (int i = 0; i < 15; i++)
{
soperators[i] = new Button();
soperators[i].Text = scientific[i];
soperators[i].Size = new Size(50, 30);
this.flowLayoutPanel1.Controls.Add(soperators[i]);
}
}
public void btn_Click(object sender,EventArgs e)
{
Button b = (Button)sender;
textBox1.Text = textBox1.Text + b.Text;
}
int firstNo;int secondNo=0;
string si;
public void op_Click(object sender,EventArgs e)
{
Int32 result;
Button b = (Button)sender;
si = b.Text;
firstNo = int.Parse(textBox1.Text);
result = firstNo;
textBox1.Text = "";
secondNo = int.Parse(textBox1.Text);
if (textBox1.Text!= "")
{
switch(si)
{
case "+":
result = firstNo + secondNo;
break;
case "-":
result = firstNo - secondNo;
break;
case "*":
result = firstNo * secondNo;
break;
case "/":
result = firstNo / secondNo;
break;
case "=":
textBox1.Text = result.ToString();
break;
}
}
}
推荐答案
private double firstValue;
private string operatorToExecute;
private void ActionOperator_Click((object sender, EventArgs e))
{
operatorToExecute = // get the operator from the button's Text property
firstValue = // parse the entry TextBox to get the first value
// clear the entry TextBox
}
private void EqualOperator_Click()
{
// do you need to check and see if you have a valid action operator ?
// do you need to check and see if you have a valid first value ?
// do you need to handle more than one click at a time of this button ?
double secondValue = // get the second value from the entry TextBox
// switch statement goes here to execute action operator
// display result or error message
}
这篇关于清空后如何在texbox中添加第二个数字?计算器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!