[英]C# halt until button is pressed
我正在用C#構建一個計算器。 我想以錯誤的聲音暫停計算器,直到按下清除按鈕。 就像在計算平方根時一樣。 是-ve。
這是計算平方根的部分
private void buttonSquareRoot_Click(object sender, EventArgs e)
{
num1 = double.Parse(textBox1.Text);
if (num1 < 0.0)
{
textBox1.Text = "Invalid Input";
}
else
{
result = Math.Sqrt(double.Parse(textBox1.Text));
textBox1.Text = Convert.ToString(result);
}
}
出現錯誤消息后,我希望程序停止直到單擊清除按鈕。 我已經做了明確的按鈕,像這樣。
private void buttonClear_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
您可以禁用所需的所有按鈕,直到再次需要它們為止。
void SetControlsAbility(bool isEnabled)
{
// for every control you need:
yourControl.Enabled = isEnabled;
}
然后
private void buttonSquareRoot_Click(object sender, EventArgs e)
{
num1 = double.Parse(textBox1.Text);
if (num1 < 0.0)
{
textBox1.Text = "Invalid Input";
SetControlsAbility(false);
}
else
{
result = Math.Sqrt(double.Parse(textBox1.Text));
textBox1.Text = Convert.ToString(result);
}
}
和
private void buttonClear_Click(object sender, EventArgs e)
{
textBox1.Text = "";
SetControlsAbility(true);
}
private void buttonSquareRoot_Click(object sender, EventArgs e)
{
num1 = double.Parse(textBox1.Text);
if (num1 < 0.0)
{
textBox1.Text = "Invalid Input";
**buttonSquareRoot.Enabled = False;**
}
else
{
result = Math.Sqrt(double.Parse(textBox1.Text));
textBox1.Text = Convert.ToString(result);
}
}
private void buttonClear_Click(object sender, EventArgs e)
{
textBox1.Text = "";
buttonSquareRoot.Enabled = True;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.