By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,435 Members | 2,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,435 IT Pros & Developers. It's quick & easy.

Restricting a windows textbox to digits only.

P: n/a
Restricting a windows textbox (edit item) to digits only.

Is there a windows option, using .NET C#, to only allow a user to enter
digits ONLY? If so, what is the flag or setting? If no, what is the best
method to manually and robustly do it?

I have a windows form with an editbox that I only want user's to enter
digits.

Thanks in advance.
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi volume,
you could capture the keypress event of the textbox and validate that the
character was a digit i.e.

private void Form1_Load(object sender, EventArgs e)
{
this.textBox1.KeyPress += new
KeyPressEventHandler(textBox1_KeyPress);
}

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}

Hope that helps
Mark R Dawson
http://www.markdawson.org


"volume" wrote:
Restricting a windows textbox (edit item) to digits only.

Is there a windows option, using .NET C#, to only allow a user to enter
digits ONLY? If so, what is the flag or setting? If no, what is the best
method to manually and robustly do it?

I have a windows form with an editbox that I only want user's to enter
digits.

Thanks in advance.

Nov 17 '05 #2

P: n/a
You can use the Validating event to validate the contents of the TextBox.
User can't select anything and even not close the form while the contents is
not validated.
textBox1.Validating += new
System.ComponentModel.CancelEventHandler(textBox1_ Validating);

private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
e.Cancel = !Regex.IsMatch(textBox1.Text, "[0-9]*");
}

"volume" <a@a.com> wrote in message
news:ek**************@TK2MSFTNGP14.phx.gbl...
Restricting a windows textbox (edit item) to digits only.

Is there a windows option, using .NET C#, to only allow a user to enter
digits ONLY? If so, what is the flag or setting? If no, what is the best
method to manually and robustly do it?

I have a windows form with an editbox that I only want user's to enter
digits.

Thanks in advance.

Nov 17 '05 #3

P: n/a
Thanks all. They both worked ace.

"Lebesgue" <no****@spam.jp> wrote in message
news:O5**************@TK2MSFTNGP09.phx.gbl...
You can use the Validating event to validate the contents of the TextBox.
User can't select anything and even not close the form while the contents is not validated.
textBox1.Validating += new
System.ComponentModel.CancelEventHandler(textBox1_ Validating);

private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
e.Cancel = !Regex.IsMatch(textBox1.Text, "[0-9]*");
}

"volume" <a@a.com> wrote in message
news:ek**************@TK2MSFTNGP14.phx.gbl...
Restricting a windows textbox (edit item) to digits only.

Is there a windows option, using .NET C#, to only allow a user to enter
digits ONLY? If so, what is the flag or setting? If no, what is the best
method to manually and robustly do it?

I have a windows form with an editbox that I only want user's to enter
digits.

Thanks in advance.


Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.