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
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.
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
*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 =
TrueEnd IfIf (tb.SelectionStart > tb.Text.Length - 2 And tb.Text.IndexOf(".") >= 0 And tb.Text.IndexOf(".") + 3 = tb.Text.Length) Thene.Handled =
TrueEnd IfIf e.Handled = False And tb.Text.Length = 0 Thentb.Text =
"$" + e.KeyChare.Handled =
Truetb.SelectionStart = 2
End IfEnd SubPrivate Sub TextKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPressIf Not (Char.IsLetter(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = TrueEnd SubPrivate Sub NumericLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.LeaveDim tb As TextBox = senderIf tb.Text.Length = 0 Then tb.Text = "$0.00"End SubNow, 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.
AHHH< Whats a Form Class and how do I get there?
Sorry!!!!
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.
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
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 :-)
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 dataIf Char.IsNumber(e.KeyChar) Thene.Handled =
FalseElseMessageBox.Show(
"plz click numbers only")e.Handled =
TrueEnd IfEnd SubPrivate Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave'format your entery during leaveing the textboxIf TextBox1.Text = "" ThenTextBox1.Text =
"$ 0.00"ElseTextBox1.Text = Format(
Double.Parse(TextBox1.Text), "$ ###,###,##0.00")End IfEnd SubPrivate Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress' textbox2 just accept lettersIf Char.IsLetter(e.KeyChar) Thene.Handled =
FalseElseMessageBox.Show(
"plz click letters only")e.Handled =
TrueEnd IfEnd SubPrivate Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click'validate textbox3Dim letters As Integer = 0Dim digits As Integer = 0Dim punctuators As Integer = 0 For Each chr As Char In TextBox3.Text
If Char.IsLetter(chr) Then
letters += 1
ElseIf Char.IsDigit(chr) Thendigits += 1
ElseIf Char.IsPunctuation(chr) Thenpunctuators += 1
End IfNextMessageBox.Show(
String.Format("statistics of your entery {3} letters = {0}{3} digits = {1} {3} punctuators = {2}", letters, digits, punctuators, vbNewLine))End Subhope that helps
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 IfEnd 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 IfEnd Sub
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.
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
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 = senderDim NChars As String = "1234567890"If NChars.IndexOf(e.KeyChar) = -1 Then' Invalid Charactere.Handled =
TrueElseIf e.Handled = False And tb.Text.Length = 0 Thentb.Text =
"$" + e.KeyChare.Handled =
Truetb.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 = senderIf tb.Text.Length = 0 Then tb.Text = "$0.00"End SubI am finding trouble with code to Place "$0.00" at load of application.
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) Thene.Handled =
TrueEnd IfTaken 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
Hello,
I am having a problem taking this code and changing it to allow negative characters.
Also, a newbie, so be gentle,
Steph