複製程式
Const Str1 As String = "abcdefghijklmnopqrstuvwxyz"
Private Sub Form_Load()
Dim R As Integer
Text1 = ""
Randomize
R = Fix(Rnd * 26) + 1
Label1 = Mid(Str1, R, 1)
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If LCase(Right(Text1, 1)) = LCase(Label1) Then
Dim R As Integer
Text1 = ""
Randomize
R = Fix(Rnd * 26) + 1
Label1 = Mid(Str1, R, 1)
End If
End Sub
正確來講 [Caption]屬性 是 [Label]物件類別 的[顯示屬性]...
就像 [TextBox]物件類別 的 [Text]屬性 一樣
然後VB的物件有個方便的功能就是物件名稱能提取該物件最常用的屬性
所以今天建立一個名稱叫"Label1"的[Label]的物件
Label1.Caption = "test"
跟
Label1 = "test"
這兩行的意思是一樣的...
至於為什麼CODE要寫在KeyUp...試試下面的
複製程式
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Debug.Print "down event" & Text1
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Debug.Print "Press event" & Text1
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Debug.Print "up event" & Text1
End Sub
就會知道...當Press被引發的時候Text的內容是空的...
所以沒辦法判斷輸入的字元...