C#暂停,直到按下按钮

时间:2013-07-08 12:41:43

标签: c#

我正在用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 = "";
    }

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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;
    }