C# Question

How to convert a textbox's text into a value?

I have this expression in my code

label3.Text = (((2 * 4.1) * 2) + (2 * textBox1.Text) + 31.6).ToString();


But I need the textBox1.Text value to evaluate the expression and the answer to be stored in the text of label3. So I should be able to change the textBox1 text to anything like 2, 3, etc and it should evaluate the expression and place the answer into label3.Text.

How can I do this?

Answer

I think I understand what you're asking.

You will need to first check that the value in the text box is a number, then use it to perform the calculation.

label3.Text = (((2 * 4.1) * 2) + (2 * Convert.ToDouble(textBox1.Text)) + 31.6).ToString();
Comments