2016-07-27 94 views
0

我想在我的VBA類中完成一個項目,並卡住了。我已經創建了大部分單詞搜索的程序,但是我無法使timerInterval函數正常工作。挑戰和代碼如下:在VBA中自動清除文本輸入

  1. 創建一個單詞搜索遊戲,允許用戶在預定的時間段內(例如5到10秒)查看一串字符。建立一個計時器來完成這一點。時間到了之後,隱藏字符串並提示用戶輸入他在字符串中看到的一個或多個單詞。例如,字符串keoixakaccessqcinmsboxeamlz包含單詞access和box。使用InStr函數確定用戶的猜測是否包含在單詞搜索字符串中。

Option Compare Database 
Option Explicit 

Private Sub cmdShow_Click() 
txtOutput.Value = "hello world" 
End Sub 




Private Sub Form_Load() 
txtOutput.Caption = Text 
If txtOutput.DefaultValue = Text Then 
Me.TimerInterval = 5000 
If txtOutput = 0 Then 
txtOutput = False 


End Sub 


Private Sub cmdCheck_click() 
Dim guess As Integer 
guess = InStr(txtOutput.Value, txtInput.Value) 
If guess = 0 Then 
    txtResult.Value = "You're terrible at this game" 
Else 
    txtResult.Value = "Good find!" 
End If 

End Sub 
+0

哪裏'子Form_Timer()'的代碼段?這就是文字清除魔法將會發生的地方。 [檢查了這一點](https://msdn.microsoft.com/en-us/library/office/ff836371.aspx) –

+0

什麼吉米史密斯說。 我會評論你的代碼雖然..所以,txtoutput可以是一個字符串,一個int和一個布爾值?這只是要求麻煩。 您應該要求變量聲明。 (VBA窗口,工具>選項)。如果沒有變量聲明,錯誤類型會讓你處理2個不同的變量,如var1和va1,並且會產生難以追蹤的錯誤。 即使對於由_ 分割的單行,您也應該使用End Ifs,直到缺少下劃線並弄亂我的代碼(並且很難在數千行中找到錯誤)爲止。 End If使其更具可讀性。 – CyberClaw

+0

@Cyber​​Claw - 您的建議並不適用。他已在代碼頂部顯示Option Explicit。 txtoutput是一種採用任何形式的文本的控件。如果/ Endif是一個很好的建議,但與他的定時器問題 – dbmitch

回答

0

假設你可以使用API​​調用 - 我喜歡這種方法更好。

您可以只需撥動您的文本控件顯示/隱藏

Option Compare Database 
Option Explicit 

' Simple delay mechanism using Win32 API 
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 

Private Sub cmdShow_Click() 
    txtOutput.Visible = true 
End Sub 


Private Sub Form_Load() 

    txtOutput.Value = "hello world" 

    Sleep 5000 ' 5000 milliseconds = 5 second delay 
    txtOutput.Visible = false 

End Sub 


Private Sub cmdCheck_click() 

    Dim guess As Integer 

    guess = InStr(txtOutput.Value, txtInput.Value) 
    If guess = 0 Then 
     txtResult.Value = "You're terrible at this game" 
    Else 
     txtResult.Value = "Good find!" 
    End If 

End Sub