如何在按下Visual C#中的特定按钮之前不断向标签添加数据?
我做了一个简单的计算器,它工作得很好。我添加了一个文本框和一个标签。文本框显示结果,而标签显示当前操作。我想一直显示我在标签中输入的值,直到我按下equal按钮得到答案。例如,我添加1+2+3 标签应显示-->1+2+3 文本框应显示-->6作为最终答案 这是我的密码如何在按下Visual C#中的特定按钮之前不断向标签添加数据?,c#,winforms,C#,Winforms,我做了一个简单的计算器,它工作得很好。我添加了一个文本框和一个标签。文本框显示结果,而标签显示当前操作。我想一直显示我在标签中输入的值,直到我按下equal按钮得到答案。例如,我添加1+2+3 标签应显示-->1+2+3 文本框应显示-->6作为最终答案 这是我的密码 namespace My_First_Calculator { public partial class Form1 : Form { Double resultVal = 0; S
namespace My_First_Calculator
{
public partial class Form1 : Form
{
Double resultVal = 0;
String operationPerformed = "";
bool isOperationPerformed = false;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button_Click(object sender, EventArgs e) //common method to other number buttons
{
if (textBox_Result.Text == "0" || isOperationPerformed)
textBox_Result.Clear();
isOperationPerformed = false;
Button button = (Button)sender;
if(button.Text == ".")
{
if(!textBox_Result.Text.Contains("."))
textBox_Result.Text = textBox_Result.Text + button.Text;
}
else
textBox_Result.Text = textBox_Result.Text + button.Text;
}
private void operator_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (resultVal != 0)
{
buttonEqual.PerformClick();
operationPerformed = button.Text;
labelCurrentOpration.Text = labelCurrentOpration.Text + resultVal + " " + operationPerformed;
isOperationPerformed = true;
}
else
{
operationPerformed = button.Text;
resultVal = Double.Parse(textBox_Result.Text);
labelCurrentOpration.Text = labelCurrentOpration.Text + resultVal + " " + operationPerformed;
isOperationPerformed = true;
}
}
private void button18_Click(object sender, EventArgs e) //buttonClaerAll
{
textBox_Result.Text = "0";
resultVal = 0;
labelCurrentOpration.Text = "";
}
private void button17_Click(object sender, EventArgs e) //buttonClear
{
textBox_Result.Text = "0";
//resultVal = 0;
}
private void button16_Click(object sender, EventArgs e) //buttonEqual
{
switch(operationPerformed)
{
case "+":
textBox_Result.Text = (resultVal + Double.Parse(textBox_Result.Text)).ToString();
break;
case "-":
textBox_Result.Text = (resultVal - Double.Parse(textBox_Result.Text)).ToString();
break;
case "*":
textBox_Result.Text = (resultVal * Double.Parse(textBox_Result.Text)).ToString();
break;
case "/":
textBox_Result.Text = (resultVal / Double.Parse(textBox_Result.Text)).ToString();
break;
default: break;
}
resultVal = Double.Parse(textBox_Result.Text);
labelCurrentOpration.Text = "";
}
}
您需要将新字符串附加到标签中的现有文本中
private void button_Click(object sender, EventArgs e)
{
if (textBox_Result.Text == "0" || isOperationPerformed)
textBox_Result.Clear();
isOperationPerformed = false;
Button button = (Button)sender;
if(button.Text == ".")
{
if(!textBox_Result.Text.Contains("."))
textBox_Result.Text = textBox_Result.Text + button.Text;
}
else
textBox_Result.Text = textBox_Result.Text + button.Text;
labelCurrentOperation.Text = labelCurrentOperation.Text + " " + button.Text;
}
private void operator_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
if (resultVal != 0)
{
button16.PerformClick();
operationPerformed = button.Text;
labelCurrentOpration.Text = labelCurrentOpration.Text + " " + resultVal + " " + operationPerformed;
isOperationPerformed = true;
}
else
{
operationPerformed = button.Text;
resultVal = Double.Parse(textBox_Result.Text);
labelCurrentOpration.Text = labelCurrentOpration.Text + " " + resultVal + " " + operationPerformed;
isOperationPerformed = true;
}
}
始终为控件命名有意义的名称!当你看到
clearButton
而不是button18
时,它会帮你——以及维护你的代码的其他人——省去头疼的事。好吧,它部分有效,但我希望标签显示我输入的所有内容,例如1+2+3+10-6@AshaneAlvis查看我的编辑。我猜有一点,因为你的方法名称不清楚。关键是你只需要将新字符串附加到现有的文本中。不,上面的答案根本不起作用。它只是复制数字,就像我按3,然后单击“添加”或“减去”按钮一样,它只是复制为33。只需查看windows 8.1中的Microsoft默认计算器,它在右上角显示到目前为止所做的操作(直到我按CE按钮清除所有字段)。我只需要实现这一点。你能帮我吗?我需要帮助。你能看一下吗?