Wish to use text box to only allow numeric Data entry

I am creating a windows form with VB expressions

How to I allow only text to be input into text box

How do I allow only numeric data entered into textbox?

How do I place a "zero" in the textbox if there is no value for the user to input?

How do I allow for Currency?

How do I place "," seperators and "$"?

i.e $ 1,123,123.00

eg.

Code: 'this is to create liabilites calculator

Dim mort. as double =textbox1.text

Dim Credit as double =textbox2.text

Dim Total as Double =Textbox3.text

Total = Mort + Credit

If i debug and do not place values in the box I get error. So I need to Have "0" by default.

Thanks

Chris

[750 byte] By [Chrisjune] at [2007-12-24]
# 1

How to I allow only text to be input into text box

as opposed to what ?

How do I allow only numeric data entered into textbox?

handle the keypress event and then check if Char.IsNumeric, Char.IsControl and if you want decimals, if the char is a . and there is no . in the text already. If this test failed, set e.Handled to true.

http://www.dotnet247.com/247reference/msgs/26/134069.aspx

This is one of many examples I found on the web

How do I place a "zero" in the textbox if there is no value for the user to input?

Handle the leave event, and if the string is empty, set it to "0"

if textBox.Text.Length = 0 then textBox.Text = "0"

something like that.

How do I place "," seperators and "$"?

Do you want them placed as you type ? That's a more complex control derived from a textbox.

Actually, if you're using VB.NET 2005, then the masked edit control will do some or all of this. Otherwise, if you're using an earlier version, see above :-)

If i debug and do not place values in the box I get error. So I need to Have "0" by default

Having a 0 is probably a nicer UI, but if you only want this because of this bug, then you should just write code to check if the textbox is empty, and act accordingly.

cgraus at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

Ok, Forgive me for being "NEW" lol

I want to "validate" I think the values of my many textbox's as either having "letter characters" or having "numeric characters" In some boxs I only want the user to only put in "numbers" elsewhere on the form I want them to just input "letters" such as thier name.

How can I get my text boxes to except only "letter Characters" and others to accept only "number Characters"?

For the text boxes that have only "numbers" in them if there is no value for them to input I want it to be "0" by default.

Also am I correct that I can use a masked textbox to show "$" infront of all "number"?

Thanks.

chris

Chrisjune at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

*grin* Everyone was new once. That's why people like me like to help newbies, we remember relying on help when we started.

Add this code to your form class.

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

Dim tb As TextBox = sender

If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And tb.Text.IndexOf(".") < 0)) Then

e.Handled = True

End If

If (tb.SelectionStart > tb.Text.Length - 2 And tb.Text.IndexOf(".") >= 0 And tb.Text.IndexOf(".") + 3 = tb.Text.Length) Then

e.Handled = True

End If

If e.Handled = False And tb.Text.Length = 0 Then

tb.Text = "$" + e.KeyChar

e.Handled = True

tb.SelectionStart = 2

End If

End Sub

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

If Not (Char.IsLetter(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True

End Sub

Private Sub NumericLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave

Dim tb As TextBox = sender

If tb.Text.Length = 0 Then tb.Text = "$0.00"

End Sub

Now, go to the forms designer. Click on a text box and on the bottom right, click the lightning flash to list events. For the KeyPress event on a numeric text box, click in the box, then click the arrow and choose 'NumericKeyPress'. For one to take only letters, choose 'TextKeyPress'. For the numeric ones, also choose 'NumericLeave' for the leave event. I think that does everything you want it to. I encourage you to use MSDN to work through the code and learn how it all works.

cgraus at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4
AHHH< Whats a Form Class and how do I get there?

Sorry!!!!

Chrisjune at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

First of all, you need to buy a book on VB.NET. I am *not* being rude, everyone has to learn sometime, but you're going to find it difficult to continue without a basic foundation that you're more likely to get from a book than from the web.

A form is another name for a window. The main window which contains your textboxes has code behind it. If you right click on the form in the design view and choose 'view code', you will see the code. Just place this code inside the class ( i.e. not at the bottom, which would be outside the class, but under another method that is already there ).

Then you can add the event handlers to call this code in your design view.

cgraus at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6
Didnt Take it as Rude!!! Thanks you for your help. I understand some of it but many of the terms are "NEW"
You have been very helpfull and I appreciated it .

Chris

Chrisjune at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7

My pleasure. I'm a moderator, so I've done it for you, but in future please try to mark an answer as correct when you've got the right answer in these forums. The forums are searched for the VS help, and marking answers as right bumps their priority in the search :-)

cgraus at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 8

hi,

yes as what cgraus told you masked box is designed for thise cases but also you can do that through code but you will write much code like this

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

' textbox1 just accept numeric data

If Char.IsNumber(e.KeyChar) Then

e.Handled = False

Else

MessageBox.Show("plz click numbers only")

e.Handled = True

End If

End Sub

Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave

'format your entery during leaveing the textbox

If TextBox1.Text = "" Then

TextBox1.Text = "$ 0.00"

Else

TextBox1.Text = Format(Double.Parse(TextBox1.Text), "$ ###,###,##0.00")

End If

End Sub

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

' textbox2 just accept letters

If Char.IsLetter(e.KeyChar) Then

e.Handled = False

Else

MessageBox.Show("plz click letters only")

e.Handled = True

End If

End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

'validate textbox3

Dim letters As Integer = 0

Dim digits As Integer = 0

Dim punctuators As Integer = 0

For Each chr As Char In TextBox3.Text

If Char.IsLetter(chr) Then

letters += 1

ElseIf Char.IsDigit(chr) Then

digits += 1

ElseIf Char.IsPunctuation(chr) Then

punctuators += 1

End If

Next

MessageBox.Show(String.Format("statistics of your entery {3} letters = {0}{3} digits = {1} {3} punctuators = {2}", letters, digits, punctuators, vbNewLine))

End Sub

hope that helps

shakalama at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 9

Here is a fairly straight-forward way to restrict your textbox to allow only numbers, $, and commas:

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

Dim allowedChars As String = "0123456789$,"

If allowedChars.IndexOf(e.KeyChar) = -1 Then
' Invalid Character
e.Handled = True
End If

End Sub

And here is how to restrict your textbox to a specific set of alphanumeric characters. Note the e.KeyChar.ToLower so uppercase alpha characters don't need to specified in the allowedChars list.

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

Dim allowedChars As String = "abcdefghijklmnopqrstuvwxyz!@#$%&()-[]{}"

If allowedChars.IndexOf(e.KeyChar.ToLower) = -1 Then
' Invalid Character
e.Handled = True
End If

End Sub

GordonBell at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 10

But this will allow a numeric value of

459.3$.34

I guess this allows you to be more explicit as to what chars to allow than char.IsXXX does, though.

cgraus at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 11

I know I am still new but!!!

I noticed that with the first posted Code I placed In the form class (see I am learning something) and that this was able to affect all textboxes! Mind you I noticed that if I were typing in code manually as apposed to copy paste this would have taken forever. It does give me what I need originally. However, the Masked Textbox's which I have set to numeric and numeric leave do not give me "$" nor "," as a seperator. A minor issue that I will play with.

In the last posted code is it possible to handle all leave events with out re doing code for each box?

Thanks for helping my learning curve everyone.

Chris

Chrisjune at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 12

Private Sub NumericKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox10.KeyPress, TextBox11.KeyPress, TextBox12.KeyPress

'This allows only numeric keypress.

Dim tb As TextBox = sender

Dim NChars As String = "1234567890"

If NChars.IndexOf(e.KeyChar) = -1 Then

' Invalid Character

e.Handled = True

ElseIf e.Handled = False And tb.Text.Length = 0 Then

tb.Text = "$" + e.KeyChar

e.Handled = True

tb.SelectionStart = 2

End If

How do I add "," as seperators for hundred, thousands, hundredthousands etc...? Other than allowing them as part of string?

eg Dim Nchars As String = "1234567890,"

Private Sub NumericLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox12.Leave, TextBox11.Leave, TextBox10.Leave

' This allows $0.00 if tabbed throw text box on Leave of textbox

Dim tb As TextBox = sender

If tb.Text.Length = 0 Then tb.Text = "$0.00"

End Sub

I am finding trouble with code to Place "$0.00" at load of application.

Chrisjune at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 13

Also this code places "." and allows only two digits after the decimal point.

If (tb.SelectionStart > tb.Text.Length - 2 And tb.Text.IndexOf(".") >= 0 And tb.Text.IndexOf(".") + 3 = tb.Text.Length) Then

e.Handled = True

End If

Taken from first posted code. I placed it after

"Dim NChars As String = "1234567890.,"

from code above that I changed slightly from earlier help.

Thanks again guys

Chrisjune at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 14

Hello,

I am having a problem taking this code and changing it to allow negative characters.

Also, a newbie, so be gentle,

Steph

LoneStar at 2007-8-31 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...