bertino12
asked on
Making a textbox only accept numbers and commas
I want to make a textbox only accept numbers and commas. What is the best way to do this?
void NumberTextBox_KeyPress() should be implemented as below. Sorry, I posted incorrect code for that.
void NumberTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (!valids.Contains(e.KeyChar))
{
e.Handled = true;
}
}
If you want backspace to be allowed, then change the char list to the following:
private readonly List<char> valids = new List<char>() { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',', '\b' };
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I get the error:
'Contains' is not a member of 'System.Array'.
'Contains' is not a member of 'System.Array'.
ASKER
Okay, Just had to change valids() to an arraylist so i could use the contains method. Thanks. It seems to be working just fine.
Open in new window