|
-
January 27th, 2009, 03:05 PM
#1
[RESOLVED] Prevent Two Decimal Points in Textbox?
Hi folks,
I have some textboxes for the entry of numbers.
I am parseing the textbox contents into double variables for calculations in the TextChanged event.
I have already used the KeyPress events to prevent any character entry other than zero to nine, the back keys, and the decimal point.
But if more than one decimal point is entered, obviously the program throws an exception.
Does anyone know how this is usually dealt with to prevent a second decimal point in the textbox?
Thankyou for any advice.....
-
January 27th, 2009, 03:57 PM
#2
Re: Prevent Two Decimal Points in Textbox?
You could use a regular expression instead. I am admittedly weak on regex's, so I am weary of giving you an example for this, but someone else can I'm sure. You could check the count of decimals and, if the count is already one, set e.Handled to true so that it is not entered.
-
January 27th, 2009, 04:56 PM
#3
Re: Prevent Two Decimal Points in Textbox?
Hi BigEd,
This is less of a hair puller than the other problem! I just am not sure how to do it. I was thinking I could create some form of loop which will check every textbox digit each time the KeyPress event fires.....but not sure how that will work. Maybe set a variable the first time a decimal point is found, and then use that to prevent another one.I can create a loop, but my string manipulation still requires a lot of learning, and that seemed like a lot of code for something like this.....
How can I check the decimal count?
Thankyou.
-
January 27th, 2009, 05:02 PM
#4
Re: Prevent Two Decimal Points in Textbox?
A regular expression is the way to go. You can simply check the text against that expression every keypress and if it doesn't match, set e.Handled to true, which tells the object that you handled the message and it will not appear in the textbox. Unfortunately, I never can remember the syntax off the top of my head, so you will need to look it up yourself or wait for someone to come along that has more knowledge than I do. I would look it up, but I am at work
-
January 27th, 2009, 05:11 PM
#5
Re: Prevent Two Decimal Points in Textbox?
Code:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '.')
{
if (textBox1.Text.Contains("."))
{
e.Handled = true;
}
}
}
Last edited by Traps; January 27th, 2009 at 05:15 PM.
-
January 27th, 2009, 05:17 PM
#6
Re: Prevent Two Decimal Points in Textbox?
Yeah, that was what I mentioned above,but I think that it would still be beneficial for the OP to look into regex validation, if only for learning purposes.
-
January 27th, 2009, 05:46 PM
#7
Re: Prevent Two Decimal Points in Textbox?
Thankyou both very much. I've added it into my code as shown and its working great. 
Code:
private void txbNewNumPerPack_KeyPress(object sender, KeyPressEventArgs e)
{
if((e.KeyChar < '0' || e.KeyChar > '9')&& e.KeyChar != '.' && e.KeyChar != (char)Keys.Back)
e.Handled = true;
if (e.KeyChar == '.')
{
if (txbNewNumPerPack.Text.Contains("."))
{
e.Handled = true;
}
}
}
I'll look into Regex, but a quick search reveals that I might need some time for it:
http://www.csharphelp.com/archives/archive21.html
I'll look into it over the next week or two. Thankyou both again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|