How to get only numbers and - in the text box?

How to get only Alphabates and . in text box?

How to get uppercase alphabate in Text box?

For the first and second one try to use MaskedTextBox

For uppercase text use the .ToUpper.ToString

You can also check if the data typed is lower case or not using the TextChanged handle like this:

For Each alph As String In TextBox1.Text
If alph = " " Then
txt = txt & " "
Else
If alph = alph.ToLower Then
alph = alph.ToString.ToUpper
txt = txt & alph
End If
End If
Next
TextBox1.Text = txt

You will have to declare the variable txt globally because if you declare it under the TextBox1_TextChanged handler it will not work accordingly and also as you can see below the for statement I was trying to handle the space but then once you type the next alphabet that will follow the space it freezes because the system will also take all the texts typed in a textbox and verify it if its a lower case and that where it freezes because the space has no upper or lower caps.

So that was an idea of how you can go about doing it also there is another method which first verify if the text entered is numeric or text so I think you can also use that.

Another way would be verifying if the text entered is number or "-" then if it one of that it can keep it else remove it from textbox that will make it only take a certain keys like numbers, and - only instead of using a MaskedTexbox.

Hope this will help you our on your project.

It is not very hard if you use regex. Not knowing if you have three textboxes or one, here is some code using one textbox and three radiobuttons to change validation of the input in textbox1.

Imports System.Text.RegularExpressions

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Private Sub validatIn()
        If RadioButton1.Checked = True Then
            Dim regex As New Regex("^[0-9-]*$")
            If regex.IsMatch(TextBox1.Text) Then
                TextBox1.Text = TextBox1.Text
            Else
                MsgBox("Textbox1 only allows numbers and - ")
            End If
        End If

        If RadioButton2.Checked = True Then
            Dim regex As New Regex("^[A-Za-z.]*$")
            If regex.IsMatch(TextBox1.Text) Then
                TextBox1.Text = TextBox1.Text
            Else
                MsgBox("Textbox1 now only allows letters and .")
            End If
        End If
                   If RadioButton3.Checked = True Then
            Dim regex As New Regex("^[A-Z]*$")
            If regex.IsMatch(TextBox1.Text) Then
                TextBox1.Text = TextBox1.Text
            Else
                MsgBox("Textbox1 now only allows Upper case letters")
            End If
        End If
    End Sub
    Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
        Me.Close()
    End Sub
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        validatIn()
    End Sub
End Class
commented: Nice! +15

The following code will allow/prevent the entry of specific keys. Add a Case statement for each key or range of keys you want to allow. Note that you have to add a Case to allow BackSpace but not Left/Right Arrow or Delete.

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True
    End Sub

    Private Sub TextBox1_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

        'Remove the following line for production code
        Me.Text = "ASC " & Asc(e.KeyChar) & "    HEX " & Asc(e.KeyChar).ToString("X2")

        Select Case e.KeyChar
            Case "a" To "z"     'lower case letter
            Case "A" To "Z"     'upper case letter
            Case "0" To "9"     'digit
            Case Chr(8)         'backspace
            Case Else:          e.Handled = True
        End Select

    End Sub

End Class

O.K. I added some lines to automatically remove the wrong entry and move the cursor back to the end of the string in the textbox. Also select radiobutton1 form load.

Imports System.Text.RegularExpressions

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RadioButton1.Checked = True
    End Sub
    Private Sub validatIn()
        If RadioButton1.Checked = True Then
            Dim regex As New Regex("^[0-9-]*$")
            If regex.IsMatch(TextBox1.Text) Then
                TextBox1.Text = TextBox1.Text
            Else
                MsgBox("Textbox1 only allows numbers and - ")
                If TextBox1.Text <> "" Then
                    TextBox1.Text = Trim(TextBox1.Text.Remove(TextBox1.Text.Length - 1))
                    TextBox1.SelectionStart = TextBox1.Text.Length
                End If
            End If
        End If

        If RadioButton2.Checked = True Then
            Dim regex As New Regex("^[A-Za-z.]*$")
            If regex.IsMatch(TextBox1.Text) Then
                TextBox1.Text = TextBox1.Text
            Else
                MsgBox("Textbox1 now only allows letters and .")
                If TextBox1.Text <> "" Then
                    TextBox1.Text = Trim(TextBox1.Text.Remove(TextBox1.Text.Length - 1))
                    TextBox1.SelectionStart = TextBox1.Text.Length
                End If
            End If
        End If
        If RadioButton3.Checked = True Then
            Dim regex As New Regex("^[A-Z]*$")
            If regex.IsMatch(TextBox1.Text) Then
                TextBox1.Text = TextBox1.Text
            Else
                MsgBox("Textbox1 now only allows Upper case letters")
                If TextBox1.Text <> "" Then
                    TextBox1.Text = Trim(TextBox1.Text.Remove(TextBox1.Text.Length - 1))
                    TextBox1.SelectionStart = TextBox1.Text.Length
                End If
            End If
        End If
    End Sub
    Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
        Me.Close()
    End Sub
    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        validatIn()
    End Sub
End Class

Minor correction - you don't need Me.KeyPress = True. I usually just use that to assign application shortcut keys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.