Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I would like to perform the following operation using a textbox and button in WPF/ Visual C#

C#
private button_click()
{
if ( textbox.text ! = integer) //something like that
{
MessageBox.Show("enter a valid number");
textbox.Text.clear();
<< get the input from user again using the same textbox and request the user to click the button again
}
else
{
newCar.cylinders = double.Parse(textBox.Text);
}
}


Any help would be much appreciated. Thanks.
Posted
Updated 13-Sep-11 5:10am
v2

1 solution

Just do this:

C#
int value;
if (Int32.TryParse(textBox.Text, out value))
{
    // do something

}
else
{
    // show error

}
 
Share this answer
 
Comments
steersteer 13-Sep-11 11:08am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900