Shane Nicholson

Shane Nicholson

  • NA
  • 36
  • 1.7k

C# How to use a textbox variable

Nov 9 2020 3:35 PM
Good day everyone,
 
First of all, thank you for taking the time to read my request, it means alot.
 
I have created a windows form on VB 2019.
 
When a user puts a number into textbox 5, I want the following equation to be calculated and the total displayed in Textbox8.
 
The below works to some level, but if the number is negative (example: -200) it does not work.
 
Also if the user tries to clear the text box, it always crashes with the same error " System.FormatException: 'Input string was not in a correct format.'
 
My guess is this has something to do with the variable I am using and cannot recognise a character.
 
Thanks again people, I really appreciate your time and expertise.
  1. private void textBox5_TextChanged(object sender, EventArgs e)  
  2. {  
  3. double firstbox;  
  4. double answer;  
  5. firstbox = double.Parse(textBox5.Text);  
  6. answer = firstbox / 1000 * 3.18141 * 2.3 * 1.19;  
  7. textBox8.Text = (answer.ToString());  
  8. }

Answers (2)