直接上代碼解釋更加清楚
//說明:本實例用到4個API函數CharLower,CharLowerBuff,CharUpper,CharUpperBuff,分別是對字符串進行大小寫轉換。進行每個字符串大小寫轉換。
用到4個按鈕,2個編輯框控件2個標簽控件。
Option Explicit
Private Declare Function CharLower Lib "user32" Alias "CharLowerA" (ByVal lpsz As String) As String
Private Declare Function CharLowerBuff Lib "user32" Alias "CharLowerBuffA" (ByVal lpsz As String, ByVal cchLength As Long) As Long
Private Declare Function CharUpper Lib "user32" Alias "CharUpperA" (ByVal lpsz As String) As String
Private Declare Function CharUpperBuff Lib "user32" Alias "CharUpperBuffA" (ByVal lpsz As String, ByVal cchLength As Long) As Long
Dim c1 As Integer '大寫的字符的個數
Dim c2 As Integer '小寫的字符的個數
Dim s1 As String '字符串
Private Sub Command1_Click()
Text2.Text = CharUpper(Text1.Text) '字符串全部轉換大寫
c2 = 0 '小寫的字符個數就為0
End Sub
Private Sub Command2_Click()
Text2.Text = CharLower(Text1.Text) '字符串全部轉換小寫
c1 = 0 '大寫字符的個數為0
End Sub
Private Sub Command3_Click()
If c1 < Len(Text1.Text) Then '如果大寫的字符個數小於編輯框字符的個數
c1 = c1 + 1 '大寫的字符個數加1
c2 = Len(Text1.Text) - c1 '小寫的個數就減一
Else
MsgBox "全部字符轉換完畢!"
Exit Sub
End If
s1 = Text1.Text '已轉換大寫編輯框1的字符串賦值給s1
CharUpperBuff s1, c1 '第一個參數表示要轉換的字符串,大寫字符的個數
Text2.Text = s1 's1賦值給編輯框2的內容
End Sub
Private Sub Command4_Click() '同上
If c2 < Len(Text1.Text) Then
c2 = c2 + 1
c1 = Len(Text1.Text) - c2
Else
MsgBox "全部字符轉換完畢!"
Exit Sub
End If
s1 = Text2.Text
CharLowerBuff s1, c2
Text2.Text = s1
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
c1 = 0
c2 = 0
End Sub
如圖所示: