1,105,242 Community Members

Numeric TextBox

Member Avatar
blazted
Newbie Poster
17 posts since Oct 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I created a text box event handler to only allow my textbox to accept Numbers only. The Event handler works great and does not allow anything but numbers except when the form first loads and the character entered is non-numeric it then allows the first charcter to be entered can be non-numberic. If I enter a number then erase it it will not allow a non-numeric character.

I saw one example of this but the texbox had a underline in it but it did not have any code in it.

Here is my event handler right now.

private void rtbQuantity_TextChanged(object sender, EventArgs e)
{
      rtbQuantity.KeyPress += new KeyPressEventHandler(rtbQuantity_KeyPress);
}

private void rtbQuantity_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar < '0') || (e.KeyChar > '9')) e.Handled = true;
}

Thank you

Member Avatar
blazted
Newbie Poster
17 posts since Oct 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Never mind figured it out.

Member Avatar
Jugortha
Junior Poster
172 posts since Oct 2007
Reputation Points: -1 [?]
Q&As Helped to Solve: 16 [?]
Skill Endorsements: 0 [?]
 
0
 

I guess that u should use this way that I give for u it is more professional
add using System.Text.RegularExpressions; to your project then
Define a Regex object
Regex FormatControl = new Regex(@"\D");
the @"\D" indicate that only numeric expressions would be accepted then and this if block

rtbQuantity
if (FormatControl.IsMatch(rtbQuantity.Text))
{
oTextBox.Text = "";
MessageBox.Show("Only numeric format would be accepted", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Member Avatar
mksinghal
Newbie Poster
2 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi,
here is the full code for creating a textbox that accepts only interger values.

www.source-code-spot.blogspot.com


I created a text box event handler to only allow my textbox to accept Numbers only. The Event handler works great and does not allow anything but numbers except when the form first loads and the character entered is non-numeric it then allows the first charcter to be entered can be non-numberic. If I enter a number then erase it it will not allow a non-numeric character.

I saw one example of this but the texbox had a underline in it but it did not have any code in it.

Here is my event handler right now.

private void rtbQuantity_TextChanged(object sender, EventArgs e)
{
      rtbQuantity.KeyPress += new KeyPressEventHandler(rtbQuantity_KeyPress);
}

private void rtbQuantity_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar < '0') || (e.KeyChar > '9')) e.Handled = true;
}

Thank you

Member Avatar
Ravenheart
Light Poster
27 posts since Nov 2009
Reputation Points: 1 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

Or you could just use a NumericUpDown control as you SHOULD have done in the first place.

Member Avatar
mksinghal
Newbie Poster
2 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi,

i use your code but when i have type in that textbox like 26354 and now i want to paste something non-numeric it will erase all data that i have already entered as 26354.

so i give you my code as given link above....

Or you could just use a NumericUpDown control as you SHOULD have done in the first place.

Member Avatar
Nick Evan
Industrious Poster
4,827 posts since Oct 2006
Reputation Points: 4,005 [?]
Q&As Helped to Solve: 560 [?]
Skill Endorsements: 30 [?]
Team Colleague
Featured
 
0
 

so i give you my code as given link above....

Stop spamming your website. We understood the first time you linked to it. Double posts were removed.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article