1

I am having three textboxes one inputting hours, one numbers and one power rate and a button but am having a formatException whenever i try to input values in the textboxes. How can i fore go this please? Whenever i compile it runs well but i am unable to input the values. Help please. Thanks!

int num1 = int.Parse(TextBox1.Text);

double num2 = double.Parse(TextBox2.Text;);

int num3 = int.Parse(TextBox3.Text;);
soln1 = num1 * num2 * num3;
MessageBox.Show(soln1.ToString());
user707727
  • 1,287
  • 7
  • 16
  • 5
    On which line you get this exception? What is your textbox values? What is your `CurrentCulture`? You have an extra `;` in your code by the way. – Soner Gönül Jul 28 '15 at 08:09
  • do you really have double.Parse(TextBox2.Text;); instead if double.Parse(TextBox2.Text); – demonplus Jul 28 '15 at 08:09

5 Answers5

2

try to change parse to convert like this

int num1 = Convert.ToInt32(TextBox1.Text);  
double num2 = Convert.ToDecimal(TextBox2.Text;);
int num3 = Convert.ToInt32(TextBox3.Text;);
soln1 = num1 * num2 * num3;
MessageBox.Show(soln1.ToString());
kulotskie
  • 331
  • 1
  • 16
0

You are trying to multiply a double and an integer. Try casting your integer to a double, and declare the variable you are going to store the result in as a double too.

If it really needs to be an integer then convert it after the calculation

DrLazer
  • 2,805
  • 3
  • 41
  • 52
0

Try

int num1 = int.Parse(TextBox1.Text);

double num2 = double.Parse(TextBox2.Text.Replace(".", ",")); // <---- 

int num3 = int.Parse(TextBox3.Text;);
soln1 = (int)(num1 * num2 * num3); // <----
MessageBox.Show(soln1.ToString());
0

Instead of using parse use TryParse, like that:

if (int.TryParse(TextBox1.Text,out num1) &&
double.TryParse(TextBox2.Text,out num2) &&
int.TryParse(TextBox3.Text,out num3))
{
   soln1 = num1 * num2 * num3;
}
israel altar
  • 1,768
  • 1
  • 16
  • 24
0

Try using:

int num1 = integer.parseInt(TextBox1.Text);
double num2 = double.Parse(TextBox2.Text);

int num3 = Integer.ParseInt(TextBox3.Text);
soln1 = num1 * num2 * num3;
MessageBox.Show(soln1.ToString);
GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104
mikyle
  • 1