cncdrive wrote:Hi Dan,
I think it would be even better to not even allow users to type in other than numbers if the textbox has to contain a number only.
Here is a little function for you which checks if the keys the user pressed are numeric characters and not allowing other characters to be inserted into the checkbox:
- Code: Select all
private void Checkifnumber(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
Just attach a Keypress Event to the textboxes of interest and call the above function.
Thanks works great, I modified slightly not to allow any decimals just numbers.
Dan