473,725 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Numeric Textbox Code

I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely reading them all - but I think I have a differing opinion of how I want to handle the 'user experience' in the application I'm creating. While I know I could allow the user to enter in number and alpha text - in a text box - and then tell them when the execuate a command "This is not numeric data", I would rather not allow them to enter alpha data - to begin with.

I've posted the below code from a page someone suggested. It is from a William Ryan. I have used it - it does work - but I run into two problems. One, the user cannot use the backspace key and two, it allows someone to enter another "." - decimal. The code author realizes this and says, "The use above will allow all valid numbers as well as decimals. It wouldn't be very difficult to expand upon this to verify that you don't have two decimal in the number, allow for currency characters etc." However - to be quite honest - I don't follow what the code is even doing. Can someone clarify what it is doing and how one would modify it to allow for only one decimal? (Bernie Yaeger I did read your post as well - if you could let me know how I could download your code/control - I would love to see it) Please see William's code below (link: http://www.knowdotnet.com/articles/n...textboxes.html

Private Overloads Sub TextBox1_TextCh anged(ByVal sender As System.Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles TextBox1.KeyPre s

Dim isKey As Boolean = e.KeyChar.IsDig it(e.KeyChar
Dim isDecimal As Boolean = e.KeyChar.ToStr ing = ".
If Not isKey And Not isDecimal The
e.Handled = Tru
End I
End Sub
Nov 20 '05 #1
11 4573
Hi Keith,

I've read your post. I'm a bit busy now but I will either .zip the code to
you or contact you and get it to you in another manner. I will get this
done hopefully sometime this weekend, Mon the latest. I will try to include
a doc explaining whatever might need explanation. I actually have 2
controls, as I may have told you - one for integers and one for currency.

But - I need to have an email address or an ftp location to send it to -
please get back to me with this at your soonest convenience. You can email
me directly at be*****@cherwel linc.com.

Bernie

"Keith" <an*******@disc ussions.microso ft.com> wrote in message
news:BA******** *************** ***********@mic rosoft.com...
I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely
reading them all - but I think I have a differing opinion of how I want to
handle the 'user experience' in the application I'm creating. While I know
I could allow the user to enter in number and alpha text - in a text box -
and then tell them when the execuate a command "This is not numeric data", I
would rather not allow them to enter alpha data - to begin with.
I've posted the below code from a page someone suggested. It is from a William Ryan. I have used it - it does work - but I run into two problems.
One, the user cannot use the backspace key and two, it allows someone to
enter another "." - decimal. The code author realizes this and says, "The
use above will allow all valid numbers as well as decimals. It wouldn't be
very difficult to expand upon this to verify that you don't have two decimal
in the number, allow for currency characters etc." However - to be quite
honest - I don't follow what the code is even doing. Can someone clarify
what it is doing and how one would modify it to allow for only one decimal?
(Bernie Yaeger I did read your post as well - if you could let me know how I
could download your code/control - I would love to see it) Please see
William's code below (link:
http://www.knowdotnet.com/articles/n...textboxes.html)
Private Overloads Sub TextBox1_TextCh anged(ByVal sender As System.Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles TextBox1.KeyPre ss
Dim isKey As Boolean = e.KeyChar.IsDig it(e.KeyChar)
Dim isDecimal As Boolean = e.KeyChar.ToStr ing = "."
If Not isKey And Not isDecimal Then
e.Handled = True
End If
End Sub

Nov 20 '05 #2
* "=?Utf-8?B?S2VpdGg=?=" <an*******@disc ussions.microso ft.com> scripsit:
I've posted the below code from a page someone suggested. It is from
a William Ryan. I have used it - it does work - but I run into two
problems. One, the user cannot use the backspace key and two, it allows
someone to enter another "." - decimal.


There is a 3rd problem: It doesn't allow me to enter a number in German
number format when I run the app on a German language system (for
example, "12,3". And it prevents me from entering a number in hex or
octal format.

What if the user already entered a "." and now wants to move it to
another position inside the number he has typed into the textbox? By
forbidding the user to enter a 2nd ".", editing will be very hard. The
user will have to delete the ".", then move to another position and
enter the "." there.

Just my 2 Euro cents...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
Hi Keith,

When you was stayed in the original thread and told there what where the
problems, than I could have given you this sample I made already some days
ago. It includes the methode which Herfried wants as well, I think this one
is one of the nicest for the customer.

I hope this helps?

Cor

\\\
Private Sub Form1_Load(ByVa l sender As System.Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
Me.TextBox1.Max Length = 10 'or whatever you want
End Sub
Private Sub textbox1_KeyUp( ByVal sender As Object, _
ByVal e As System.Windows. Forms.KeyEventA rgs) Handles textbox1.KeyUp
If e.KeyValue <> 8 Then
If Not IsNumeric(TextB ox1.Text) Then
MessageBox.Show ("Only numeric is allowed")
TextBox1.Select ionStart = TextBox1.Text.L ength - 1
TextBox1.Select ionLength = 1
End If
End If
End Sub
Private Sub TextBox1_Valida ting(ByVal sender _
As Object, ByVal e As System.Componen tModel.CancelEv entArgs) _
Handles TextBox1.Valida ting
If Not IsNumeric(TextB ox1.Text) Then
MessageBox.Show ("There was an error pasting")
TextBox1.Focus( )
End If
End Sub
///

Nov 20 '05 #4
Try this Keith:

Private Sub TextBox3_KeyPre ss(ByVal sender As Object, ByVal e As
System.Windows. Forms.KeyPressE ventArgs) Handles TextBox3.KeyPre ss
If e.KeyChar.IsDig it(e.KeyChar) OrElse e.KeyChar = Chr(Keys.Back)
Then Exit Sub
If e.KeyChar.IsPun ctuation(e.KeyC har) AndAlso
TextBox3.Text.I ndexOf(".") = -1 Then Exit Sub
e.Handled = True

End Sub
"Keith" <an*******@disc ussions.microso ft.com> wrote in message
news:BA******** *************** ***********@mic rosoft.com...
I apologize for those of you who think I'm posting on the same topic. It is not that I don't appreciate all of your comments - and I'm definitely
reading them all - but I think I have a differing opinion of how I want to
handle the 'user experience' in the application I'm creating. While I know
I could allow the user to enter in number and alpha text - in a text box -
and then tell them when the execuate a command "This is not numeric data", I
would rather not allow them to enter alpha data - to begin with.
I've posted the below code from a page someone suggested. It is from a William Ryan. I have used it - it does work - but I run into two problems.
One, the user cannot use the backspace key and two, it allows someone to
enter another "." - decimal. The code author realizes this and says, "The
use above will allow all valid numbers as well as decimals. It wouldn't be
very difficult to expand upon this to verify that you don't have two decimal
in the number, allow for currency characters etc." However - to be quite
honest - I don't follow what the code is even doing. Can someone clarify
what it is doing and how one would modify it to allow for only one decimal?
(Bernie Yaeger I did read your post as well - if you could let me know how I
could download your code/control - I would love to see it) Please see
William's code below (link:
http://www.knowdotnet.com/articles/n...textboxes.html)
Private Overloads Sub TextBox1_TextCh anged(ByVal sender As System.Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles TextBox1.KeyPre ss
Dim isKey As Boolean = e.KeyChar.IsDig it(e.KeyChar)
Dim isDecimal As Boolean = e.KeyChar.ToStr ing = "."
If Not isKey And Not isDecimal Then
e.Handled = True
End If
End Sub

Nov 20 '05 #5
Oops, mistake. Now it will work

Private Sub TextBox3_KeyPre ss(ByVal sender As Object, ByVal e As
System.Windows. Forms.KeyPressE ventArgs) Handles TextBox3.KeyPre ss
If e.KeyChar.IsDig it(e.KeyChar) OrElse e.KeyChar = Chr(Keys.Back)
Then Exit Sub
If e.KeyChar = "."c AndAlso TextBox3.Text.I ndexOf(".") = -1 Then
Exit Sub

e.Handled = True

End Sub


"yEaH rIgHt" <nospam@haha> wrote in message
news:10******** *****@corp.supe rnews.com...
Try this Keith:

Private Sub TextBox3_KeyPre ss(ByVal sender As Object, ByVal e As
System.Windows. Forms.KeyPressE ventArgs) Handles TextBox3.KeyPre ss
If e.KeyChar.IsDig it(e.KeyChar) OrElse e.KeyChar = Chr(Keys.Back)
Then Exit Sub
If e.KeyChar.IsPun ctuation(e.KeyC har) AndAlso
TextBox3.Text.I ndexOf(".") = -1 Then Exit Sub
e.Handled = True

End Sub
"Keith" <an*******@disc ussions.microso ft.com> wrote in message
news:BA******** *************** ***********@mic rosoft.com...
I apologize for those of you who think I'm posting on the same topic. It
is not that I don't appreciate all of your comments - and I'm definitely
reading them all - but I think I have a differing opinion of how I want to
handle the 'user experience' in the application I'm creating. While I know I could allow the user to enter in number and alpha text - in a text box -
and then tell them when the execuate a command "This is not numeric data", I would rather not allow them to enter alpha data - to begin with.

I've posted the below code from a page someone suggested. It is from a William Ryan. I have used it - it does work - but I run into two

problems. One, the user cannot use the backspace key and two, it allows someone to
enter another "." - decimal. The code author realizes this and says, "The
use above will allow all valid numbers as well as decimals. It wouldn't be very difficult to expand upon this to verify that you don't have two decimal in the number, allow for currency characters etc." However - to be quite
honest - I don't follow what the code is even doing. Can someone clarify
what it is doing and how one would modify it to allow for only one decimal? (Bernie Yaeger I did read your post as well - if you could let me know how I could download your code/control - I would love to see it) Please see
William's code below (link:
http://www.knowdotnet.com/articles/n...textboxes.html)

Private Overloads Sub TextBox1_TextCh anged(ByVal sender As
System.Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles

TextBox1.KeyPre ss
Dim isKey As Boolean = e.KeyChar.IsDig it(e.KeyChar)
Dim isDecimal As Boolean = e.KeyChar.ToStr ing = "."
If Not isKey And Not isDecimal Then
e.Handled = True
End If
End Sub


Nov 20 '05 #6
* "Cor Ligthert" <no**********@p lanet.nl> scripsit:
ago. It includes the methode which Herfried wants as well, I think this one
is one of the nicest for the customer. [...] If Not IsNumeric(TextB ox1.Text) Then
MessageBox.Show ("There was an error pasting")
TextBox1.Focus( )


I do not like messageboxes. This will prevent the user from leaving the
control if he/she is not able to enter valid data.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7
Hi Herfreid,

I can't believe you're sticking to your position. First, you know very well
that you can program around the minor problems you mention. Second, if I
want you to fill out a form in black ink, I give you the form and a black
pen. If I give you a red pen as well and you use it, do I tell you at the
end that the form was to be filled out in black ink and ask you to fill it
out again?

Bernie

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:el******** ******@tk2msftn gp13.phx.gbl...
* "=?Utf-8?B?S2VpdGg=?=" <an*******@disc ussions.microso ft.com> scripsit:
I've posted the below code from a page someone suggested. It is from
a William Ryan. I have used it - it does work - but I run into two
problems. One, the user cannot use the backspace key and two, it allows
someone to enter another "." - decimal.


There is a 3rd problem: It doesn't allow me to enter a number in German
number format when I run the app on a German language system (for
example, "12,3". And it prevents me from entering a number in hex or
octal format.

What if the user already entered a "." and now wants to move it to
another position inside the number he has typed into the textbox? By
forbidding the user to enter a 2nd ".", editing will be very hard. The
user will have to delete the ".", then move to another position and
enter the "." there.

Just my 2 Euro cents...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #8
Hi Keith,

I became crazy from that Herfried (is not true), in the sample is a problem
when you set it to blank or would leave the program with an error. I changed
that.

Remember it is just a sample, how you do your error is your sake, you can
use by instance an errorprovider for that, but that would make the sample
less simple to show, while this one becomes already more and more difficult
to show

(statified with the text of the errroprovider now Herfried?)

Cor
\\\\
Private Sub Form1_Load(ByVa l sender As System.Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
Me.TextBox1.Max Length = 10
End Sub
Private Sub textbox1_KeyUp( ByVal sender As Object, _
ByVal e As System.Windows. Forms.KeyEventA rgs) Handles textbox1.KeyUp
If e.KeyValue <> 8 Then
If Not IsNumeric(TextB ox1.Text) Then
If TextBox1.Text.L ength > 0 Then
MessageBox.Show ("Only 1 to 10 is allowed")

TextBox1.Select ionStart = TextBox1.Text.L ength - 1
TextBox1.Select ionLength = 1
End If

End If
End If
End Sub
Private Sub TextBox1_Valida ting(ByVal sender _
As Object, ByVal e As System.Componen tModel.CancelEv entArgs) _
Handles TextBox1.Valida ting
If TextBox1.Text.L ength > 0 Then
If Not IsNumeric(TextB ox1.Text) Then
MessageBox.Show ("There was an error pasting")
TextBox1.Focus( )
e.Cancel = True
End If
End If
End Sub
////
Nov 20 '05 #9
* "Bernie Yaeger" <be*****@cherwe llinc.com> scripsit:
I can't believe you're sticking to your position. First, you know very well
that you can program around the minor problems you mention. Second, if I
want you to fill out a form in black ink, I give you the form and a black
pen. If I give you a red pen as well and you use it, do I tell you at the
end that the form was to be filled out in black ink and ask you to fill it
out again?


Paper forms are very different from forms on the computer. You can
provide context-sensitive help for the control and display information
about how to fill in the values there. In an application, we used an
errorprovider with an icon showing a "?" on a blue filled square that
displayed information on what to will in. If the input was not valid,
we showed an errorprovider.

I didn't say that there should not be any guidance at all. There should
be hints, and in some special cases custom controls make sense (for
example, DateTimePicker) . But even this control has its problems and
limitations. What I like is if the control reformats the input after
successfully validating its contents, but even that can be annoying.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4654
by: Jose Gonzalez | last post by:
How to apply a numeric format to a textbox using xhtml? I know you have to use the "-wap-input-format" style tag in css. I can get this to work in a regular xhtml page, however, I've been unsuccessful in getting it to work in asp.net 1.1 The following code has allowCustomAttributes set and works fine for labels, but not for textboxes. This is because it is building span tags around the control. I need to embed the class tag inside the...
2
7396
by: Patrick De Ridder | last post by:
MSDN shows the option: textBox1.Numeric = true; as a way to check that the input in textBox1 is numeric. I cannot get it to work. How should it be done? Alternatively: looking at Validation in MSDN I see a check for an Internet Address, I cannot convert the example to a check for Numeric Input. Please help. --
2
5553
by: Sam | last post by:
Why isn't there a numberBox Windows Form control like a textbox control but for numeric (float) input? There doesn't seem to be any easy or visual solution to this VERY SIMPLE ISSUE in either version of Visual Studio. I simply wanted to create a Windows app where a user could enter miles or kilometers, gallons or liters, and see the other in the adjacent box. The form was easy but the textbox control would not let me cast any number of...
7
3938
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34 123.0000 123 i.e. I want to trim trailing zeros and (decimal point if no decimal values
16
8400
by: Keith | last post by:
Am I crazy - to be shocked that there is no Numeric, Alpha, and AlphaNumeric Property in on the Textbox control I searched and searched - and found other people's code - but they don't work perfectly. Some do only allow numeric - but you cannot backspace. Maybe that is "good enough" - but surely it is not that difficult to have a textbox that only accepts numeric values and backspaces.
1
1913
by: Luqman | last post by:
My textbox is bound to Typed Dataset, with a Numeric Field, I want the user should not type anything in textbox except Numeric, how can I ? The data type is : Sql Server Decimal(5,2) Currently, VB.Net automatically raise error on lost focus of textbox, but I don't want user to type anything else except Numeric, any property to be set in Textbox?
5
3645
tolkienarda
by: tolkienarda | last post by:
hi all i am trying to stop people from entering anything but numbers into a textbox. i have the code to make sure they have entered a number on each keypress event and now a msgbox appears if they enter a non numeric value but how do i stop the character from appearing in the textbox. here is my code Private Sub txtA_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtA.KeyPress,...
7
6833
by: nussu | last post by:
Hi, Plz provide me javascript : javascript for a textbox to accept the values in decimals and within range i need to enter a value in textbox like 1.03 and it should be <3 (lessthan 3). Plz help me. Rgds,
5
28637
lotus18
by: lotus18 | last post by:
Hello World! I have a sample code here written in vb .net that restricts the textbox to accept only alpha, alphanumeric or numeric characters. Public Enum MyOption Alpha = 1 AlphaNumeric = 2 Numeric = 3 End Enum
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.