Results 1 to 34 of 34

Thread: a numbers only textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    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

  2. #2
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    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...

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    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....

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: a numbers only textbox


  5. #5
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: a numbers only textbox


  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    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?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    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

  8. #8
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: a numbers only textbox

    Oh i never knew it creates the control.

  9. #9

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    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?

  10. #10
    Lively Member
    Join Date
    May 2008
    Posts
    117

    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?


  11. #11
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question Re: a numbers only textbox

    Couldn't you just use a NumericUpDown control?
    ~Peter


  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: a numbers only textbox

    the link that Noah posted is for a decimal textbox. if you only want integers, try this:

    vb Code:
    1. Public Class intTextbox
    2.  
    3.     Inherits TextBox
    4.  
    5.     Const WM_PASTE As Integer = &H302
    6.  
    7.     Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
    8.         e.Handled = Not (Char.IsControl(e.KeyChar) OrElse Char.IsDigit(e.KeyChar))
    9.         MyBase.OnKeyPress(e)
    10.     End Sub
    11.  
    12.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    13.         If m.Msg = WM_PASTE Then
    14.             Dim strText As String = Me.Text
    15.             strText = strText.Remove(Me.SelectionStart, Me.SelectionLength)
    16.             strText = strText.Insert(Me.SelectionStart, Clipboard.GetText)
    17.             Dim result As Integer = 0
    18.             If Not Integer.TryParse(strText, result) Then
    19.                 Return
    20.             End If
    21.         End If
    22.         MyBase.WndProc(m)
    23.     End Sub
    24.  
    25. End Class

  13. #13
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: a numbers only textbox

    Quote Originally Posted by MrGTI
    Couldn't you just use a NumericUpDown control?
    I second this.

    Stop re-inventing the wheel
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: a numbers only textbox

    the NumericUpDown control doesn't have the right visual appearance. i wouldn't use it

  15. #15
    Addicted Member
    Join Date
    Mar 2008
    Posts
    143

    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!

  16. #16
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    Re: a numbers only textbox

    Couldn't he just use a maskTextBox instead?

  17. #17
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Cool Re: a numbers only textbox

    Yes - a MaskedTextBox or a NumericUpDown both work great, and require no extra code.
    ~Peter


  18. #18
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: a numbers only textbox

    Quote 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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: a numbers only textbox

    Quote 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?

  20. #20

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    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?

  21. #21
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    Re: a numbers only textbox

    Quote 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.

  22. #22
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: a numbers only textbox

    Hi,

    You can try this:

    vb Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.         Dim asciiInt As Integer = Asc(e.KeyChar)
    3.         'MsgBox(asciiInt) -Uncomment to see what the asc value is on each key press
    4.         Select Case asciiInt
    5.             Case 48 To 57 'Numbers
    6.                 e.Handled = False
    7.             Case 8 'BackSpace
    8.                 Call Beep()
    9.                 e.Handled = True
    10.             Case 32 'Space
    11.                 Call Beep()
    12.                 e.Handled = True
    13.             Case 97 To 122 'Letters
    14.                 Call Beep()
    15.                 e.Handled = True
    16.             Case 65 To 90 'Capital Letters
    17.                 Call Beep()
    18.                 e.Handled = True
    19.             Case 45 'Dashes
    20.                 Call Beep()
    21.                 e.Handled = True
    22.             Case 59 'semi-colon  
    23.                 Call Beep()
    24.                 e.Handled = True
    25.             Case Else 'everything else...
    26.                 Call Beep()
    27.                 e.Handled = True
    28.         End Select
    29.     End Sub

    Use just a textbox and this code and find out.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  23. #23
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  24. #24

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    Re: a numbers only textbox

    yeah I thought about a numeric text box too but I don't like the way it looks either.

  25. #25
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    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.

  26. #26
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: a numbers only textbox

    Quote 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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  27. #27
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    UK
    Posts
    489

    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)

  28. #28

    Thread Starter
    New Member
    Join Date
    Dec 2007
    Posts
    9

    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?

  29. #29
    Addicted Member Zero2Cool's Avatar
    Join Date
    Aug 2006
    Location
    Green Bay, WI
    Posts
    203

    Re: a numbers only textbox

    Quote 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.

  30. #30
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    Re: a numbers only textbox

    Hello Zero2Cool,

    In VB2005 how do you make a Masked Textbox numeric only?

    Best Rgds,
    Tarablue

  31. #31
    Hyperactive Member Tarablue's Avatar
    Join Date
    Feb 2007
    Location
    Somewhere West of GMT
    Posts
    398

    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

  32. #32
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    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:
    1. 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:
    1. Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    2.         If e.KeyCode = Keys.OemPeriod Or e.KeyCode = Keys.Decimal Then
    3.             e.SuppressKeyPress = True
    4.             SendKeys.Send(",")
    5.         End If
    6.     End Sub

  33. #33
    Member
    Join Date
    Nov 2008
    Posts
    63

    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.

  34. #34
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    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
  •  



Click Here to Expand Forum to Full Width