שתף באמצעות


Restrict textbox to range of numbers in Visual Basic Express .net

Question

Tuesday, July 2, 2013 11:38 AM

hi,

so i have a program. but in a textbox i want to restrict the user to only input the numbers 1 to 5. i already made the textbox so that it only accepts a number.

please help :)

Edit: Thanks for the great responses worked really well.

All replies (8)

Tuesday, July 2, 2013 3:37 PM ✅Answered | 1 vote

Maybe this?

Public Class Form1

    Dim i As Integer = 0

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "Testing for Integer in TextBox"
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Try
            i = Integer.Parse(TextBox1.Text, Globalization.NumberStyles.Any)
        Catch ex As Exception
            Dim ToolTip1 As New ToolTip
            ToolTip1.IsBalloon = True
            ToolTip1.UseFading = True
            ToolTip1.ToolTipIcon = ToolTipIcon.Error
            ToolTip1.ToolTipTitle = "ONLY USE INTEGERS FROM 1 TO 5"
            ToolTip1.Show("Style is not an Integer value", TextBox1, New Point(0, -80), 2500)
            TextBox1.Text = ""
        End Try
        If i >= 1 AndAlso i <= 5 Then
            Exit Sub
        Else
            Dim ToolTip1 As New ToolTip
            ToolTip1.IsBalloon = True
            ToolTip1.UseFading = True
            ToolTip1.ToolTipIcon = ToolTipIcon.Error
            ToolTip1.ToolTipTitle = "ONLY USE INTEGERS FROM 1 TO 5"
            ToolTip1.Show("Value was out of range", TextBox1, New Point(0, -80), 2500)
            TextBox1.Text = ""
        End If
    End Sub

End Class

You've taught me everything I know but not everything you know. _________________________________________________________________________________________________________________ This search engine is for MSDN Library and has many features. http://social.msdn.microsoft.com/Search/en-US?query=search%20msdn%20library&beta=0&ac=8


Tuesday, July 2, 2013 11:45 AM | 1 vote

hi,

so i have a program. but in a textbox i want to restrict the user to only input the numbers 1 to 5. i already made the textbox so that it only accepts a number.

please help :)

You can do that by handling the keypress event but I wouldn't do it that way. If the user isn't aware of the restriction, they'll think your program is broken.

Instead, consider handling the .Validating event. In that event check that it's a valid integer (Integer.TryParse) and if so, that the range is between 1 and 5. If not, then show them a message indicating the restrictions.

Please call me Frank :)


Tuesday, July 2, 2013 11:47 AM

Hi

Maybe this?

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        Dim tb As TextBox = DirectCast(sender, TextBox)
        Dim v As Integer = 0
        If Integer.TryParse(tb.Text, v) Then
            If v < 1 Or v > 5 Then
                tb.Clear()
            End If
        Else
            tb.Clear()
        End If
    End Sub

Regards Les, Livingston, Scotland


Tuesday, July 2, 2013 3:51 PM

Option Strict On
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.MaxLength = 1
    End Sub
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        Select Case True
            Case Char.IsDigit(ChrW(e.KeyCode))
                If "12345".IndexOf(ChrW(e.KeyCode)) < 0 Then
                    e.Handled = True
                    e.SuppressKeyPress = True
                End If
            Case e.KeyCode = Keys.Back
            Case e.KeyCode = Keys.Delete
            Case Else
                e.Handled = True
                e.SuppressKeyPress = True
        End Select
    End Sub
End Class

“If you want something you've never had, you need to do something you've never done.”

Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles

*This post does not reflect the opinion of Microsoft, or its employees.


Tuesday, July 2, 2013 6:21 PM

I have tried it and worked really well. although whenever i click on another textbox the error of "not an integer" pops up even when it is a valid integer between 1 & 5

my code:

Dim Userint as Integer 

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Try
            i = Integer.Parse(TextBox1.Text, Globalization.NumberStyles.Any)
        Catch ex As Exception
            Dim ToolTip1 As New ToolTip
            ToolTip1.IsBalloon = True
            ToolTip1.UseFading = True
            ToolTip1.ToolTipIcon = ToolTipIcon.Error
            ToolTip1.ToolTipTitle = "ONLY USE INTEGERS FROM 1 TO 5"
            ToolTip1.Show("Style is not an Integer value", TextBox1, New Point(0, -80), 2500)
            TextBox1.Text = ""
        End Try
        If i >= 1 AndAlso i <= 5 Then
            Userint = i
        Else
            Dim ToolTip1 As New ToolTip
            ToolTip1.IsBalloon = True
            ToolTip1.UseFading = True
            ToolTip1.ToolTipIcon = ToolTipIcon.Error
            ToolTip1.ToolTipTitle = "ONLY USE INTEGERS FROM 1 TO 5"
            ToolTip1.Show("Value was out of range", TextBox1, New Point(0, -80), 2500)
            TextBox1.Text = ""
        End If
    End Sub

@JustJort


Tuesday, July 2, 2013 6:42 PM

I realize that you passed over what I suggested without notice, but I'll show you what I meant anyway:

Option Strict OnOption Explicit On'Public Class Form1    '    '    Private ep As New ErrorProvider    '    '    Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) _        Handles MyBase.Load        '        btn_GoToNextStep.Enabled = False        '    End Sub    '    '    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _        Handles TextBox1.TextChanged        '        ep.Clear()        '        If TextBox1.Text.Trim <> "" Then            btn_GoToNextStep.Enabled = True        Else            btn_GoToNextStep.Enabled = False        End If        '    End Sub    '    '    Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _        Handles TextBox1.Validating        '        If TextBox1.Text.Trim <> "" Then            Dim tempInt As Integer = 0            Dim sb As New System.Text.StringBuilder            '            If Not Integer.TryParse(TextBox1.Text, tempInt) Then                sb.AppendLine("The value entered is not")                sb.AppendLine("a valid whole number.")                sb.AppendLine()                sb.AppendLine("Please re-enter the value")                sb.AppendLine("or clear the textbox entirely")                sb.AppendLine("in order to continue.")                '                ep.SetError(TextBox1, sb.ToString)                '                e.Cancel = True                btn_GoToNextStep.Enabled = False            Else                If tempInt < 1 Then                    sb.AppendLine("The minimum value that")                    sb.AppendLine("you can enter is 1.")                    sb.AppendLine()                    sb.AppendLine("Please re-enter the value")                    sb.AppendLine("or clear the textbox entirely")                    sb.AppendLine("in order to continue.")                    '                    ep.SetError(TextBox1, sb.ToString)                    '                    e.Cancel = True                    btn_GoToNextStep.Enabled = False                ElseIf tempInt > 5 Then                    sb.AppendLine("The maximum value that")                    sb.AppendLine("you can enter is 5.")                    sb.AppendLine()                    sb.AppendLine("Please re-enter the value")                    sb.AppendLine("or clear the textbox entirely")                    sb.AppendLine("in order to continue.")                    '                    ep.SetError(TextBox1, sb.ToString)                    '                    e.Cancel = True                    btn_GoToNextStep.Enabled = False                End If            End If        End If        '    End Sub    '    '    Private Sub btn_GoToNextStep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _        Handles btn_GoToNextStep.Click        '        MessageBox.Show(TextBox1.Text)        '    End Sub    '    'End Class

For what it's worth...

Please call me Frank :)


Wednesday, July 3, 2013 4:06 AM

I have tried it and worked really well. although whenever i click on another textbox the error of "not an integer" pops up even when it is a valid integer between 1 & 5

my code:

Dim Userint as Integer 

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Try
            i = Integer.Parse(TextBox1.Text, Globalization.NumberStyles.Any)
        Catch ex As Exception
            Dim ToolTip1 As New ToolTip
            ToolTip1.IsBalloon = True
            ToolTip1.UseFading = True
            ToolTip1.ToolTipIcon = ToolTipIcon.Error
            ToolTip1.ToolTipTitle = "ONLY USE INTEGERS FROM 1 TO 5"
            ToolTip1.Show("Style is not an Integer value", TextBox1, New Point(0, -80), 2500)
            TextBox1.Text = ""
        End Try
        If i >= 1 AndAlso i <= 5 Then
            Userint = i
        Else
            Dim ToolTip1 As New ToolTip
            ToolTip1.IsBalloon = True
            ToolTip1.UseFading = True
            ToolTip1.ToolTipIcon = ToolTipIcon.Error
            ToolTip1.ToolTipTitle = "ONLY USE INTEGERS FROM 1 TO 5"
            ToolTip1.Show("Value was out of range", TextBox1, New Point(0, -80), 2500)
            TextBox1.Text = ""
        End If
    End Sub

@JustJort

I don't see i created as an integer in your code so I don't know how TextBox1 text changed event is working.

And, probably, without seeing all of your code I doubt I could assist you with resolving the issue.

Although it seems to work fine for me. Just make sure you change all of the information in the code, if you copy it to a new textboxs text changed event, for the new textbox. Such as the tooltips control to use. Or the textbox to perform the Integer.Parse on or the textbox to clear if an error occurs.

Public Class Form1

    Dim i As Integer

    Dim Userint As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "Testing"
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        Try
            i = Integer.Parse(TextBox1.Text, Globalization.NumberStyles.Any)
        Catch ex As Exception
            Dim ToolTip1 As New ToolTip
            ToolTip1.IsBalloon = True
            ToolTip1.UseFading = True
            ToolTip1.ToolTipIcon = ToolTipIcon.Error
            ToolTip1.ToolTipTitle = "ONLY USE INTEGERS FROM 1 TO 5"
            ToolTip1.Show("Style is not an Integer value", TextBox1, New Point(0, -80), 2500)
            TextBox1.Text = ""
        End Try
        If i >= 1 AndAlso i <= 5 Then
            Userint = i
            Me.Text = Userint.ToString
        Else
            Dim ToolTip1 As New ToolTip
            ToolTip1.IsBalloon = True
            ToolTip1.UseFading = True
            ToolTip1.ToolTipIcon = ToolTipIcon.Error
            ToolTip1.ToolTipTitle = "ONLY USE INTEGERS FROM 1 TO 5"
            ToolTip1.Show("Value was out of range", TextBox1, New Point(0, -80), 2500)
            TextBox1.Text = ""
        End If
    End Sub

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
        Try
            i = Integer.Parse(TextBox2.Text, Globalization.NumberStyles.Any)
        Catch ex As Exception
            Dim ToolTip1 As New ToolTip
            ToolTip1.IsBalloon = True
            ToolTip1.UseFading = True
            ToolTip1.ToolTipIcon = ToolTipIcon.Error
            ToolTip1.ToolTipTitle = "ONLY USE INTEGERS FROM 1 TO 5"
            ToolTip1.Show("Style is not an Integer value", TextBox2, New Point(0, -80), 2500)
            TextBox2.Text = ""
        End Try
        If i >= 1 AndAlso i <= 5 Then
            Userint = i
            Me.Text = Userint.ToString
        Else
            Dim ToolTip1 As New ToolTip
            ToolTip1.IsBalloon = True
            ToolTip1.UseFading = True
            ToolTip1.ToolTipIcon = ToolTipIcon.Error
            ToolTip1.ToolTipTitle = "ONLY USE INTEGERS FROM 1 TO 5"
            ToolTip1.Show("Value was out of range", TextBox2, New Point(0, -80), 2500)
            TextBox2.Text = ""
        End If
    End Sub

End Class

You've taught me everything I know but not everything you know. _________________________________________________________________________________________________________________ This search engine is for MSDN Library and has many features. http://social.msdn.microsoft.com/Search/en-US?query=search%20msdn%20library&beta=0&ac=8


Thursday, December 5, 2013 4:09 AM

Hi Good day, i just want ask how to change restricted input numbers (1 to 5) to (65 to 100)

Thanks a lot....