First off, you're attempting to parse the textbox itself into these types, not the value inside the textbox. You should be using the Text
property on the textbox to get the value:
int.Parse(textBox1.Text); //Note the .Text
Second, you're not assigning the result of the operation to the textbox. To assign a value to a textbox, again use the Text
property:
textBox1.Text = int.Parse(textBox1.Text).ToString();
Third, this is going to fail if what is inside the textbox is not valid for the type you select. For instance, if you enter "hello" into the textbox and select int
the code will crash because you can't parse "hello" into an int
. You can remedy this by catching a FormatException
.
try
{
int.Parse(textBox1.Text);
}
catch (FormatException fe)
{
//Display an error
}
Alternatively you can use the TryParse
methods on each type (int.TryParse
)
Fourth, in some instances you won't notice any thing change anyway. If you enter 123
into the textbox and parse it to an int
and replace the textbox value, it will still say 123
because that's what the string representation of the int
value 123
is.
Fifth, what are you trying to accomplish here? This code basically does nothing.