I am new to C#. I\'m trying to make a calculator, but the following error occurred:
Input string was not in a correct format.
T
Since you have cleared the textbox on the previous line, the Parse conversion fails.
textBox1.Text = String.Empty;
num2 = double.Parse(textBox1.Text);
How will it convert String.Empty to Double? The way of doing it is not right. For example, if the "+" button is clicked, you have to check whether there was already a number. If so,, add the numbers and display the result:
Double num;
private void Add_Click(object sender, EventArgs e)
{
If (num != null)
{
num == num + Convert.ToDouble(textBox1.Text);
}
else
{
num1 == Convert.ToDouble(textBox1.Text);
}
textBox1.Text = num;
}
Seems like num2
value should be fetched from textbox2
not textbox1
(You are setting textbox1.text to empty and trying to parse it to double again)
//You are setting textbox1 to empty
textBox1.Text = String.Empty;
//here trying to parse it to double
num2 = double.Parse(textBox1.Text);
Also don't use Convert.ToDouble(textBox1.Text)
directly. if users type non numeric values your code will crash. first check if its a valid number, always use doube.TryPrase()
double num1;
double.TryParse(textBox1.Text, out num1);
Your code seems hard to understand whats the use of button_13 and button_14;
I will assume your trying to do this:
bool ifNew = true;
double num1 ,num2,result;
private void Add_Click(object sender, EventArgs e))
{
if(ifNew)
{
num1 = Convert.ToDouble(textBox1.Text);
textbox1.Clear();
ifNew = false;
result += num1;
}
else
{
num2 = Convert.ToDouble(textBox1.Text);
textbox1.Clear();
result += num2;
num1 = 0D;
num2 = 0D;
ifNew = true;
}
}
private void Equals_Click(object sender, EventArgs e)
{
textboxl.Text = string.Format("{0:N}",result);
}
Or you could use Double.Parse in my Convert.ToDouble It depends on your operation but I am visualizing how to do add operation you can change and edit this depends on your operation
What are you trying to achieve with this code? It seems that your algorythm is wrong.
Like others said, this code
textBox1.Text = String.Empty;
num2 = double.Parse(textBox1.Text);
will throw an Exception because an empty string cannot be converted to Double!
So, I'm wondering why did you reset your field. I thought about it for a while, and maybe I got what are you trying to do. Let's say you type a number in TextBox1. Then you press the "-" button to subtract and then you want to enter the second number to view the result. Is this the case? If it is, the code you wrote is not going to wait for your next input!
In fact, when you click the button, it just executes all the lines you wrote. I'd write something like this instead.
double num1, num2, result;
string operation;
private void button14_Click(object sender, EventArgs e) //Minus Button
{
if (textBox1.Text != String.Empty) //Added if statement to see if the textBox is empty
num1 = Convert.ToDouble(textBox1.Text);
else
num1 = 0; //If textBox is empty, set num1 to 0
textBox1.Text = String.Empty;
operation = "-";
}
private void button13_Click(object sender, EventArgs e) //Equals Button
{
if (textBox1.Text != String.Empty)
num2 = Convert.ToDouble(textBox1.Text);
else
num2 = 0;
if (operation == "-")
{
result = num1 - num2;
textBox1.Text = Convert.ToString(result);
}
if (operation == "+")
{
//You got it
}
//And so on...
}
EDIT: If the string is empty, this is going to always throw Exceptions, so I added a control. If the string is empty, value becomes zero.
Also remember that the Parse method is relying on the culture of your operating system to perform the conversion, so try to change your code to
num2 = double.Parse(textBox1.Text, CultureInfo.InvariantCulture);
You might also consider to use the
double.TryParse
method for better exception handling.