1

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

2 回答 2

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);
}
于 2013-07-08T12:45:41.393 回答
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;
    }
于 2013-07-08T12:44:53.827 回答