Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert textbox value to double in c#?

Tags:

c#

I am new to c# and using windows forms. I have 2 textboxes textbox1 and textbox2.

Lets say textbox1 has a value of 22, when I click on textbox2, the value in textbox1 should change to double (22.00).

textbox1.text = "22";
private void textBox2_MouseClick(object sender, MouseEventArgs e)
{
    // convert the value in textbox1 to double .
}
like image 208
Kate Avatar asked Dec 25 '22 09:12

Kate


1 Answers

private void textBox2_MouseClick(object sender, MouseEventArgs e)
{
     // TODO: handle exceptions
     textBox1.Text = double.Parse(textBox1.Text).ToString("F2");
}

1) You can find the format strings here: https://msdn.microsoft.com/pl-pl/library/dwhawy9k(v=vs.110).aspx.

2) double.Parse(...) can throw an exception: https://msdn.microsoft.com/pl-pl/library/fd84bdyt(v=vs.110).aspx

like image 118
romanoza Avatar answered Jan 11 '23 17:01

romanoza