I have a project in vb.net which i would like to create a program that a textbox will only allow alphabet,space and dots and it is in uppercase only.. no special character and numbers allowed. Thank you and God bless.. Hope to help me..

You will have to make use of the textChanged event.

You will need to keep track of the key pressed also.

Create an array filled with all of the characters/keys you want to filter, then check against it with the key pressed.

If it passes,add it to the text box.

Private Sub TextBox2_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyUp
        If e.KeyCode < 65 Or e.KeyCode > 90 Then
            MsgBox("No Numbers please")
            Exit Sub
        End If
    End Sub

This code needs some make-up and decorations. Better first customize then use.

This code you can use for conversion of string to uppercase during a lost focus event. Same way, you will find Lowercase, Propercase and other string manupulation functions.

 Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
        TextBox1.Text = StrConv(TextBox1.Text, VbStrConv.Uppercase)
 End Sub

ascii Table web site will give you the Ascii key codes for most characters - you can then use the KeyCode example given by bnitishpai above to filter out what you don't want.

For example, this code will block them entering the invalid text altogether.

Private sub Textbox1_OnKeyPress(byVal sender as object, byval e as System.EventArgs) Handles Textbox1.KeyPress
dim AsciiCode as integer  = e.keycode    
If AsciiCode = 32 OrElse AsciiCode = 46 then
    '32 is a space, 46 is a .
    e.Handled = true 'Allow input       
elseif AsciiCode => 65 AndAlso AsciiCode <= 90 then
    '65 = Upper Case A , 90 is Upper case Z
    e.Handled = true 'allow input
    e.Handled = False ' block input
end if

Alternatively, you could let them enter lower case but return the upper case character.

Edited 4 Years Ago by G_Waddell: spelling

space allowed here.

If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
            Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
            And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
            Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
            'space accepted
            If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
                e.Handled = True
            End If
        End If
        If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then

            e.Handled = False
        End If

i think you just have to change the value of CharacterCasing to "Upper" this can be found in your textbox property....

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Asc(e.KeyChar) < 65 Or Asc(e.KeyChar) > 90 And Asc(e.KeyChar) < 97 Or Asc(e.KeyChar) > 122 Then
            'space accepted
            If Asc(e.KeyChar) <> 32 Then
                e.Handled = True
            End If
        End If

        If Asc(e.KeyChar) = 46 Then
            e.Handled = False
        End If

        If Asc(e.KeyChar) = 8 Then
            e.Handled = False
        End If
    End Sub
This article has been dead for over six months. Start a new discussion instead.