在VB中,字母以ASCII形式存儲,而大小寫字母的ASCII值相差32,這樣的話,如果一個大寫字母的ASCII值加上32就會變為對應的小寫字母,而小寫字母的ASCII值減去32就會變成大寫字母。在VB中實現大小寫轉換的源代碼如下:
'大寫字母轉小寫字母
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= 65 And KeyAscii <= 90 Then
KeyAscii = KeyAscii + 32
End If
End Sub
'小寫字母轉大寫字母
Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii >= 97 And KeyAscii <= 122 Then
KeyAscii = KeyAscii - 32
End If
End Sub