• 0

[VB] Limiting numerical characters in textboxes


Question

I am wondering how I would go amonst making a textbox limted to only numerical characters, and if something else was to be entered a message box will popup saying numbers only, then once the user selects "OK" for the chracters within that textbox to be highlighted.

If it is lengthly to make the data in the textbox highlighted after the user selects OK, then thats fine :) -- But the limiting to only numbers and a message box alert is needed.

Thanks in advance! :)

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

No offense meant, but it could get very annoying, very quickly for users of your program if, every time they press a non-numeric character, a message box pops up.

Anyway, let's assume your textbox is called Text1. And you're using VB6, right?

Something like this will handle only accepting numbers.

Private Sub Text1_KeyPress(KeyAscii As Integer)
 Select Case KeyAscii
  Case 0 To 31
    ' Control characters, ignore.
  Case 48 To 57
    ' Numbers, ignore.
  Case Else
    Msgbox "Please enter numbers only."
  End Select
End Sub

And something like this will highlight the text. Just stick it in the Click event of your OK button:

Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)

Haven't tested any of this, but it should work... :)

Link to comment
Share on other sites

  • 0

Just one thing.. That warning can become VERY irritating after some time.. Like the user know he's only supposed to enter numbers, but makes a mistake.. the first or second time that's ok, but after the user uses the program for a long time... He won't like it one bit.

Keep in mind that most people won't hit enter or spacebar or whatever to close the messagebox, they use the mouse... so they have to reach for the mouse, close the messagebox and move the hand to the keyboard again.

Link to comment
Share on other sites

  • 0

there is a way to do it by calling an API function, that will only allow numbers and if the user presses a letter it doesnt even register.. so there is no message box needed, or any KeyUp or KeyPress coding to be done

i just cant remember what it is, and cant find the source code that i used it in........

Link to comment
Share on other sites

  • 0

Or if you're doing it in VB.NET, you can do something like this.

Public Class RestrictiveTextBox
    Inherits System.Windows.Forms.TextBox

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Not IsNumeric(e.KeyChar) Then
            e.Handled = True
            Msgbox("You used a letter! How dare you!")
        End If
    End Sub

End Class

Link to comment
Share on other sites

  • 0

Ah perfect everyone!

Thanks for the help here :)

--

About the Message Box Annoyance thing, I totally agree on that 100%. This was a project for school, and the teacher said to impliment a way to tell the user he/she made a mistake when filling out a text box. Since this Text box was only to have a numerical value, I thought this would be a good way. :)

--

Thanks again! :)

Link to comment
Share on other sites

  • 0

You could have a small label near the entry or use a status bar to put on a message that an incorrect character was entered instead of popping up the message.

Link to comment
Share on other sites

  • 0
I am wondering how I would go amonst making a textbox limted to only numerical characters, and if something else was to be entered a message box will popup saying numbers only, then once the user selects "OK" for the chracters within that textbox to be highlighted.

If it is lengthly to make the data in the textbox highlighted after the user selects OK, then thats fine :) -- But the limiting to only numbers and a message box alert is needed.

Thanks in advance! :)

VB6.

Private Sub ButtonOk_Click
If Not IsNumeric(textbox1.text) Then 'checks for only numbers
MsgBox("Only Numeric values are allowed. Try again")
textbox1.Setfocus 'Gives the text box focus
textbox1.SelStart = 0 'Places the cursor at the beginning of the textbox
End if
End Sub.

This works too. Sorry its not all super sexy as anyone elses. You asked for something simple. :cry:

Edited by xStainDx
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.