2013-07-08 223 views
1

我正在c#中构建一个计算器。我想用错误声音暂停计算器,直到按下清除按钮。就像在计算平方根时一样,是-ve。C#停止,直到按下按钮

这里的计算平方根

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 = ""; 
    } 
+0

是不工作呢? – swapneel

+1

添加一个默认为false的'private bool IsHalted',并在每次需要暂停时都变为true。在整个代码中添加检查以检查值和抱怨,然后返回(如果值为真) –

+1

当您说您希望应用程序暂停时,您是否指禁用除Clear按钮之外的任何其他输入? – Jacques

回答

0
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; 
    } 
1

您可以禁用需要,直到再次需要它们的所有按钮。

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); 
} 
+0

其解决。我做了一个布尔变量,并用它来控制我的程序。 thnx所有的想法。 – umuieme

相关问题