hi

i am trying to accepte text in text box only numbers and capital alphabets in keypress event

below code is not converting lower case to upper case

Private Sub Tbx_AcctCode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Tbx_AcctCode.KeyPress
        Dim KeyAscii As Short

        KeyAscii = Asc(e.KeyChar)
        If KeyAscii >= 65 And KeyAscii <= 90 Or KeyAscii >= 48 And KeyAscii <= 57 Or _
 KeyAscii >= 97 And KeyAscii <= 122 Or _
 KeyAscii = 13 Or _
 KeyAscii = 8 Then
        Else
            e.Handled = True
        End If
' change lower case to upper case  - NOT WORKING
        If KeyAscii >= 97 And KeyAscii <= 122 Then
            KeyAscii = KeyAscii - 32
        End If


    End Sub

I think that is because all you are doing is subtracting 32 of KeyAscii. You're not doing anything with KeyAscii after that, remember it is just a variable you defined. You actually need to take that converted character and append it to the characters in the text box.

Rather, if your exclusion code is working and the text box only accepts letters and numbers, why don't you just use toUpper() to convert the string to uppercase?

hericles has a point. It would be easier if you use textbox.text.toupper but if you must, absolutely must,convert lower case to upper case during the keypress event then here it is

Private Sub Tbx_AcctCode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Tbx_AcctCode.KeyPress        
    Dim keyChar As Char = e.KeyChar

    If AscW(keyChar) >= 97 And AscW(keyChar) <= 122 Then
            e.KeyChar = (keyChar.ToString.ToUpper)
    Else
        If Not AscW(keyChar) >= 65 And AscW(keyChar) <= 90 Then
            If Not AscW(keyChar) >= 48 And AscW(keyChar) <= 57 Then
                If Not AscW(keyChar) = 8 Or AscW(keyChar) = 9 Or AscW(keyChar) = 13 Then
                    e.Handled = True
                End If
            End If
        End If
    End If
End Sub

Edited 5 Years Ago by bluehangook629: clean up code

hi bluehangook629

thanks for your code. it gives desired result.

I need this code several times in my program. can u advise how can i can make sub or function for this code.

Thanks

you dont even need to do that.
all you need to do is add the name of the control you want this code to execute at the end of event handler declaration.
for example if you want to execute this code with textbox1, textbox2, textbox3 then you would write your declaration like following.

Private Sub Tbx_AcctCode_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Tbx_AcctCode.KeyPress, textbox1.KeyPress, textbox2.KeyPress, textbox3.KeyPress          
        ''code code code
        ''code code code
End Sub

Edited 5 Years Ago by bluehangook629: n/a

You can also have your code run different ways by using sender

''this is an example of how you would handle each control differently
if sender is Tbx_AcctCode then
     ''code
elseif sender is textbox1 then
     ''code
elseif sender is textbox2 then
     ''code
elseif sender is textbox3 then
     ''code
end if

Textbox to enter account code i.e " Tbx_AcctCode " is added in different forms . to avoide typing same code in all forms i want to create function or sub and call as i used to do in VB6.


i dint get your below code how to work with it.

''this is an example of how you would handle each control differently
if sender is Tbx_AcctCode then    
 ''code
else
if sender is textbox1 then 
    ''code
elseif sender is textbox2 then   
  ''code
elseif sender is textbox3 then  
   ''code
end if

then you would probably need a class or a module since sharing subroutine between forms is not recommended.

Public Class Key_Press
    Public Shared Sub onlyAlphaNumeric(sender as system.object, e as system.windows.forms.keypresseventargs)
        ''put your code here
       
    End Sub
End Class
Imports "yourProjectName".Key_Press
imports system.windows.forms

Public Class "yourFormName"
    ''This is to call
    Private Sub Tbx_AcctCode_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Tbx_AcctCode.KeyPress       
        onlyAlphaNumeric(sender,e)
    End Sub
End Class

Edited 5 Years Ago by bluehangook629: n/a

This question has already been answered. Start a new discussion instead.