[VB] usage of NumericUpDown

Manix

Well-Known Member
Reaction score
29
Hello,

I have recently started programming with Visual Basic (2008 Express Edition) and I'm creating a program in which I need the user to input NUMBERS ONLY. After googling my issue I figured that I need to use NumericUpDown function or whatever it is called. My question is, since I'm a complete noob, how do I use it? I mean how do I make input of numbers only possible in a textbox?

Thanks in advance,

Manix.
 

seph ir oth

Mod'n Dat News Jon
Reaction score
262
I'm not big in VB, so I don't know how this would go down, but write some code so that if the user types a key, and the text box was selected/inputted into when that key was typed, then you check what that key is (ASCII code ftw: http://www.asciitable.com/) and if it's not the range of numbers for 0-9, which is 48 through 59, then clear the text box of that key.

The NumericUpDown bit seems to be made for small numbers, though I'm sure you could make a second set of up-down arrows to make it increase by 10's, or 100's, etc.

http://www.exforsys.com/tutorials/vb.net-2005/domainupdown-and-numericupdown-in-vb.net-2005.html
 

Sintoras

Shaaakaa!
Reaction score
45
Or, if you want to do it by code, use a normal textbox, check if the input is numeric (textbox1.text.IsNumeric), if not, delete the last character, otherwise leave it.
 

Im_On_56k

Hm...
Reaction score
116
Try something like this

Code:
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not Char.IsNumber(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub


EDIT-
This allows backspace:
Code:
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Char.IsNumber(e.KeyChar) Or e.KeyChar = ChrW(Keys.Back) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top