none
Getting number from text box RRS feed

All replies

  • Only allow integer as shown below.

    Public Class Form1
        Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
            If System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "[^0-9\s]") Then
                e.Handled = True
            End If
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then
                Dim value As Integer = CInt(TextBox1.Text)
                MessageBox.Show($"{value}")
            End If
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, June 30, 2018 12:04 AM
    Moderator
  • Use Integer.TryParse(TextBoxN.Text, intVar) where TextBoxN is the name of your textbox and intVar is an integer variable you've declared.  If TryParse returns false then the user did not enter an integer and you can show a messagebox telling them that the input is invalid instead of continuing with the routine.

    The problem with catching KeyPress is that they can still paste an invalid entry into the textbox.

    You could also handle the TextBox.Validating event to perform the TryParse test and cancel the validation if the value is incorrect.

    Private Sub TextBox1_Validating(sender As Object, e As CancelEventArgs) Handles TextBox1.Validating
        If TextBox1.TextLength > 0 Then
            If Not Integer.TryParse(TextBox1.Text, Nothing) Then
                e.Cancel = True
                TextBox1.SelectAll()
            End If
        End If
    End Sub
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, June 30, 2018 12:24 PM
    Moderator
  • Hi Tunnix

    I think you can Use ASCII

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
        Private Sub SurroundingSub()
            Dim str As String = TextBox1.Text
    
            For Each c As Char In str
    
                If Convert.ToInt32(c) >= 48 AndAlso Convert.ToInt32(c) <= 57 Then
                    TextBox2.AppendText(c)
                End If
            Next
    
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            SurroundingSub()
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 2, 2018 1:38 AM