How to not user enter "0001" in text box

the_one2003a

Member
Joined
May 15, 2005
Messages
10
Programming Experience
Beginner
I wanted to make a text box that only accepts numbers and doesn't allow zeroes to be entered in first of the user input (for example doesn't allow: 0009).

Below is my code:



Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

If Not (e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = ".") Then

e.Handled = True

Else

TextBox1.Text = TextBox1.Text().TrimStart("0")

End If



End Sub


This code works very well but when I enter "123" in the text box and then press home and "0", Zero (0) is show in the text one time L



How can I avoid the zero to be showed at first of this text box?



Thanks
 
To accomplish what you're asking by modifying your code you could try:
VB.NET:
[color=Blue]Private Sub[/color] TextBox1_KeyPress([color=Blue]ByVal[/color] sender [color=Blue]As Object[/color], [color=Blue]ByVal[/color] e As System.Windows.Forms.KeyPressEventArgs) [color=Blue]Handles[/color] TextBox1.KeyPress
[color=Blue]  If Me[/color].TextBox1.SelectionStart = 0 [color=Blue]Then[/color]
	e.Handled = e.KeyChar = "0"c [color=Blue]OrElse[/color] ([color=Blue]Not[/color] [color=Blue]Char[/color].IsDigit(e.KeyChar) [color=Blue]AndAlso[/color] e.KeyChar <> "."c)
[color=Blue]  Else[/color]
	e.Handled = [color=Blue]Not Char[/color].IsDigit(e.KeyChar) [color=Blue]AndAlso[/color] e.KeyChar <> "."c
[color=Blue]  End If[/color]

[color=Blue]  If[/color] e.Handled [color=Blue]Then[/color]
	Beep()
[color=Blue]  End If[/color]
[color=Blue] End Sub[/color]
I added the Beep() so that the user gets some feedback when they press an invalid key.

There are a few issues with this rather simplistic approach, though.
</p>
  1. If someone is entering a decimal number between 0 and 1 they should be able to enter a "0" as the first character.
  2. The user is not prevented from entering more than 1 decimal point.
  3. The user cannot copy, paste, backspace or delete in the field as these are all illegal keys.

A better idea is probably to create much more sophisticated validation or use a control created for this purpose by another developer. There are plenty available for free from various web sites. I use, and have extended, the controls detailed in an article in Visual Studio Magazine. You need to register to download code, but it's worth it as it is a good resource.

You can register here: http://www.ftponline.com/vsm

You can find the article and download the code here: http://www.ftponline.com/vsm/2005_01/magazine/columns/gettingstarted

I would higly recommend all VB and VS developers to check it out.
 
Thanks but the Registering costs money and I can't currently pay online

So can you suggest me a link to a component that you tell but it be free

thanks
 
I'm afraid you are mistaken. Use the previous URL and click the "Create Account" link at the top, or else go here:

http://www.ftponline.com/members/default.aspx?SID=&ReturnUrl=

Enter only the required information and don't request subscriptions to any of the publications. It's probably those that require payment. You don't need to be a subscriber to access the web site. You just need to have an account. Note that it states FREE access at the top of the registration page. I forgot to mention that those controls are written in C#. You could also try the Code Project web site at:

http://www.codeproject.com/

where there are several validating TextBoxes available written in VB.NET.
 
Last edited:
Back
Top