2016-07-27 97 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