|
-
Aug 31st, 2008, 04:18 PM
#1
Thread Starter
New Member
a numbers only textbox
hello everybody, this is my first post although i've learned very much from the forum. I am having problems trying to allow only numbers to be entered into my textbox, this is a n00b mistake i know but i really can't figure it out. i know it has to be in the textbox_keydown event but i really need some other opinions.
my code is below.. what am i doing wrong?
Code:
Private Sub txtPsx_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPsx.KeyDown
Select Case e.KeyValue
Case Keys.A To Keys.Z
e.Handled = False
MsgBox("Please enter a number.", MsgBoxStyle.OkOnly)
Case Keys.D0 To Keys.D9
e.Handled = True
Case Else
e.Handled = True
End Select
End Sub
-
Aug 31st, 2008, 05:00 PM
#2
Frenzied Member
Re: a numbers only textbox
Before i looked at your code the first thing that came to my mind was e.handled = false. Though it doesn't work...
-
Aug 31st, 2008, 05:03 PM
#3
Thread Starter
New Member
Re: a numbers only textbox
yeah thats what i was thinking. i don't understand. i've tried a variety of if...then statements and cases too but i just can't get it to work....
-
Aug 31st, 2008, 05:49 PM
#4
Re: a numbers only textbox
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 31st, 2008, 10:43 PM
#5
Frenzied Member
Re: a numbers only textbox
-
Sep 1st, 2008, 06:48 PM
#6
Thread Starter
New Member
Re: a numbers only textbox
well, that's getting me closer, but i'm still a n00b lol and i need some direction with the last code snippet from Pauls signature?
-
Sep 1st, 2008, 07:01 PM
#7
Re: a numbers only textbox
download the class + add it to your project. run your project, then stop it. you'll find numericTextbox at the top of your toolbox. add a numericTextbox control to your project + use it like a textbox
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 1st, 2008, 07:03 PM
#8
Frenzied Member
Re: a numbers only textbox
Oh i never knew it creates the control.
-
Sep 1st, 2008, 10:25 PM
#9
Thread Starter
New Member
Re: a numbers only textbox
alright so i copied the code into another class and all, but when i run the program i don't see the created textbox, or i might be asking: how do i link the code to the textbox i want to apply it to?
-
Sep 2nd, 2008, 01:48 AM
#10
Lively Member
Re: a numbers only textbox
Its not to link to, its a new textbox, one with "numeric-only"-bahaviour.
add the numericTextbox-class-file to ur project, then compile. After that u can find it in the toolbox and u can drag it on ur form.
@paul: good work. I'll drop my own and take urs. thx!
Last edited by Nerd44; Sep 2nd, 2008 at 01:52 AM.
whats the benefit of beeing rated?

-
Sep 2nd, 2008, 09:51 AM
#11
Frenzied Member
Re: a numbers only textbox
Couldn't you just use a NumericUpDown control?
~Peter

-
Sep 2nd, 2008, 10:36 AM
#12
Re: a numbers only textbox
the link that Noah posted is for a decimal textbox. if you only want integers, try this:
vb Code:
Public Class intTextbox
Inherits TextBox
Const WM_PASTE As Integer = &H302
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
e.Handled = Not (Char.IsControl(e.KeyChar) OrElse Char.IsDigit(e.KeyChar))
MyBase.OnKeyPress(e)
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_PASTE Then
Dim strText As String = Me.Text
strText = strText.Remove(Me.SelectionStart, Me.SelectionLength)
strText = strText.Insert(Me.SelectionStart, Clipboard.GetText)
Dim result As Integer = 0
If Not Integer.TryParse(strText, result) Then
Return
End If
End If
MyBase.WndProc(m)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 2nd, 2008, 10:40 AM
#13
Re: a numbers only textbox
 Originally Posted by MrGTI
Couldn't you just use a NumericUpDown control?
I second this.
Stop re-inventing the wheel
-
Sep 2nd, 2008, 10:46 AM
#14
Re: a numbers only textbox
the NumericUpDown control doesn't have the right visual appearance. i wouldn't use it
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 2nd, 2008, 10:53 AM
#15
Addicted Member
Re: a numbers only textbox
There is a property for text box - TextBox.Numeric
Did you try setting this to true?
-------------------------------------------------------
Ignore me - it is too early.
Namespace: System.Web.UI.MobileControls
Shut up and eat your banana!
-
Sep 2nd, 2008, 11:08 AM
#16
Addicted Member
Re: a numbers only textbox
Couldn't he just use a maskTextBox instead?
-
Sep 2nd, 2008, 12:21 PM
#17
-
Sep 2nd, 2008, 02:09 PM
#18
Re: a numbers only textbox
 Originally Posted by .paul.
the NumericUpDown control doesn't have the right visual appearance. i wouldn't use it
Ok, so you drop one on the form, set a couple of properties and viola! you have a NUD that looks and works like a TextBox with no code to maintain.
-
Sep 2nd, 2008, 02:14 PM
#19
Re: a numbers only textbox
 Originally Posted by JuggaloBrotha
Ok, so you drop one on the form, set a couple of properties and viola! you have a NUD that looks and works like a TextBox with no code to maintain.
how about an example then?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 2nd, 2008, 02:19 PM
#20
Thread Starter
New Member
Re: a numbers only textbox
well all those things sound like a good idea, i'm at school now and messing around with VB 2005 i've noticed there is no properity textbox.numeric so i don't know if it will be there on vb2008. a masked textbox would work but is that just a control that i'll drag onto my form?
-
Sep 2nd, 2008, 02:23 PM
#21
Addicted Member
Re: a numbers only textbox
 Originally Posted by amp1928
a masked textbox would work but is that just a control that i'll drag onto my form?
The same as dragging a TextBox1 control.
Last edited by Zero2Cool; Sep 2nd, 2008 at 02:31 PM.
-
Sep 2nd, 2008, 02:29 PM
#22
Re: a numbers only textbox
Hi,
You can try this:
vb Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim asciiInt As Integer = Asc(e.KeyChar) 'MsgBox(asciiInt) -Uncomment to see what the asc value is on each key press Select Case asciiInt Case 48 To 57 'Numbers e.Handled = False Case 8 'BackSpace Call Beep() e.Handled = True Case 32 'Space Call Beep() e.Handled = True Case 97 To 122 'Letters Call Beep() e.Handled = True Case 65 To 90 'Capital Letters Call Beep() e.Handled = True Case 45 'Dashes Call Beep() e.Handled = True Case 59 'semi-colon Call Beep() e.Handled = True Case Else 'everything else... Call Beep() e.Handled = True End Select End Sub
Use just a textbox and this code and find out.
Wkr,
sparrow1
-
Sep 2nd, 2008, 04:17 PM
#23
Re: a numbers only textbox
what about paste into textbox????
Code:
Dim validCH() As String = New String() {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
Dim textReent As Boolean = False, lstCH As Integer
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If textReent OrElse TextBox1.TextLength = 0 Then Exit Sub
textReent = True : lstCH = TextBox1.SelectionStart
For x As Integer = TextBox1.TextLength - 1 To 0 Step -1
If Array.IndexOf(validCH, TextBox1.Text.Substring(x, 1)) = -1 Then
TextBox1.Text = TextBox1.Text.Remove(x, 1)
lstCH = x
End If
Next
textReent = False
TextBox1.SelectionStart = lstCH : TextBox1.SelectionLength = 0
End Sub
-
Sep 3rd, 2008, 10:47 PM
#24
Thread Starter
New Member
Re: a numbers only textbox
yeah I thought about a numeric text box too but I don't like the way it looks either.
-
Sep 4th, 2008, 07:17 AM
#25
Addicted Member
Re: a numbers only textbox
I still side with using a MaskedTextBox. And if you want that underline removed, enter a space in the PromptChar property where the _ currently resides.
-
Sep 4th, 2008, 07:39 AM
#26
Re: a numbers only textbox
 Originally Posted by .paul.
how about an example then?
Now that I look at it again, I could've sworn that you could turn off the spinners in the box, or perhaps it was another control that I saw that in.
Edit: This code should hide those spinners, thus leaving you with the textbox portion:
Code:
Me.NumericUpDown1.Controls(0).Visible = False
The point is, when it comes to numeric input, everyone's modifying the textbox control of which there's a lot of ways to handle inputting data into a textbox that I simply don't see the point in adding code to handle every case that could possibly happen. Not to mention, what if the control changes later on? You'll need to go back and re-test every possible way to put data into the TB.
Besides, the NUD is a textbox with the spinners on it. If you really, really don't like them you could make a custom control that inherits the NUD and remove them yourself, but then again why do all that work just to remove a very user friendly thing?
Last edited by JuggaloBrotha; Sep 5th, 2008 at 08:12 AM.
-
Sep 4th, 2008, 08:24 AM
#27
Hyperactive Member
Re: a numbers only textbox
Couldnt you use Tryparse to dermine if its a numeric value?
(Sorry cant remember syntax without VS, dam holidays)
Learning C♯
Data Binding & Bound Controls - Objects and wizards will never be as intelligent as you, do it yourself! (Unless your Pro)
-
Sep 4th, 2008, 02:05 PM
#28
Thread Starter
New Member
Re: a numbers only textbox
when i use the masked textbox, i did notice the underscored characters. how do i remove them? when i just deleted them when setting the mask properity the textbox no longer accepts any input at all. is there code i need to add or should i put 5 spaces in if i want only 5 numbers inputed?
-
Sep 4th, 2008, 02:46 PM
#29
Addicted Member
Re: a numbers only textbox
 Originally Posted by amp1928
when i use the masked textbox, i did notice the underscored characters. how do i remove them? when i just deleted them when setting the mask properity the textbox no longer accepts any input at all. is there code i need to add or should i put 5 spaces in if i want only 5 numbers inputed?
In the properties of your MaskedTextBox, just below where you can change the font, there is a "PromptChar" field.
Delete the "_" and enter a " " space in its place. It works, I've done it several times just today
Last edited by Zero2Cool; Sep 4th, 2008 at 03:06 PM.
-
Sep 18th, 2008, 09:11 PM
#30
Hyperactive Member
Re: a numbers only textbox
Hello Zero2Cool,
In VB2005 how do you make a Masked Textbox numeric only?
Best Rgds,
Tarablue
-
Sep 18th, 2008, 10:05 PM
#31
Hyperactive Member
Re: a numbers only textbox
Hi All,
OK worked it out. Now I have set it to be Right justified but when I run the program it always comes up as Centre justified.
Best Rgds,
Tarablue
-
Sep 26th, 2008, 06:31 PM
#32
Frenzied Member
Re: a numbers only textbox
I know you figured this out (mark this thread as resolved), but i was anwsering someone elses question and while i was playing arround with the KeyEventArgs i found this:
vb Code:
e.SuppressKeyPress = True
The above code will do what you, and i though e.handled = false did.
It will cancel the key the user pressed. Remember it is True not False.
The user asked how to make it so when the user clicks the decimal/period on your keyboard the program will read it as a comma...or something like that.
Here was the code i supplied the user (it's for a textbox):
vb Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.OemPeriod Or e.KeyCode = Keys.Decimal Then
e.SuppressKeyPress = True
SendKeys.Send(",")
End If
End Sub
-
Nov 3rd, 2008, 10:31 AM
#33
Member
Re: a numbers only textbox
I was thinking, what if the control involved here is not a textbox but a datagridview??? How do i ensure that the value entered into a specified column in the datagridview is only numbers?
Your help will be greatly appreciated.
-
Nov 3rd, 2008, 07:27 PM
#34
Frenzied Member
Re: a numbers only textbox
First of all i think your question might go into a new thread.
Second i think you can use my code, click the link in my signature for more details. You can use my code on the KeyDown event of any control (well most).
Still see my signature.
Although it says Change what user clicked on his/her keyboard. you can manipulate the code and use the same conscept.
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
|