I'm building a calculator in c#. I want to halt the calculator with error sound until clear button is pressed. Like while calculating square root, the no. is -ve.
Here's the part of calculating square root
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);
}
}
After error message i want program to halt until clear button is clicked. i have already made clear button which goes like this.
private void buttonClear_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
You can disable all buttons you need until you need them again.
void SetControlsAbility(bool isEnabled)
{
// for every control you need:
yourControl.Enabled = isEnabled;
}
Then
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);
}
}
And
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.