Textfield is nummeric ?

Jelmer

Regular
Joined
Jan 11, 2006
Messages
96
I've got a textifield, i like to control if its a nummeric input if the user changes the text. It should be a rounded number or a double with 2 digits

how can i control if its a nummeric input?
 
You will have to check if the value they input is valid in either one of the Key events, or in one of the Validation events. If you are using the 2.0 framework this can be done using the double.TryParse() method. If you are using the 1.0/1.1 framework you will have to use double.Parse() and catch the exception.
Alternatively if you change one of the key events of the textbox you can allow only the number keys.
 
The double data type also supports TryParse in .Net 1.x. The difference is that in 1.x, double is the only data type that supports TryParse, whereas in .Net 2.0, most native types support it.
 
Thanx.. is works with this code:

private void txtPrijs_Validating(object sender, CancelEventArgs e)
{
double prijs;
if (Double.TryParse(txtPrijs.Text,out prijs)){
}else{
MessageBox.Show("Voer een correcte prijs in!");
txtPrijs.Text = "";
}

}
 
Back
Top