Excel Userform文本框行为

时间:2012-04-11 22:15:40

标签: events textbox excel-vba setfocus userform

我在userform上有一个文本框。它是表单上唯一的文本框。除此文本框外,还有三个标签和两个按钮。基本上,我希望焦点在所有场景下都保留在这个文本框中,除了点击其中一个按钮的那一刻,但我希望焦点能够直接回到文本框。两个按钮都将“TakeFocusOnClick”和“TabStop”设置为False。将焦点设置到文本框时出现问题,这就是我更改这两个设置的原因。

一旦我更改了这些设置,文本框中的Enter键就会停止生效。我为文本框编写了_AfterUpdate和_KeyPress的事件,但它们没有触发。正如您在代码中看到的那样,我已经注释掉了将焦点设置到此文本框的行。由于它现在是唯一可以聚焦的对象,因此不需要这些线(理论上)。当我允许其他对象进行聚焦时,这些线条没有任何效果(尽管有这些SetFocus线,焦点仍然切换到按钮)。

这是代码。这很简单,只是Enter键没有触发事件。谁能明白为什么?感谢。

Private Sub btnDone_Click()
    Application.Calculation = xlCalculationAutomatic
    formMath.Hide

    'Clear statistics
    Range("attempts").Value = 0
    Range("correct").Value = 0
    Sheet5.Range("A2:W500").ClearContents

End Sub


Private Sub btnSubmit_Click()
    recordAnswer
    'formMath.txtAnswer.SetFocus
End Sub

Private Sub txtAnswer_AfterUpdate()
    recordAnswer
    'formMath.txtAnswer.SetFocus
End Sub

Private Sub txtAnswer_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 13 Then
        recordAnswer
    End If
End Sub

Private Sub UserForm_Initialize()

    'Initialize manual calculation
    Application.Calculation = xlCalculationManual
    Application.Calculate

    'Initialize statistics
    Range("attempts").Value = 0
    Range("correct").Value = 0
    Sheet5.Range("A2:W500").ClearContents

    'Initialize first problem
    newProblem

End Sub

Sub recordAnswer()

    'Update statistics
    Dim attempts, correct As Integer
    attempts = Range("attempts").Value
    correct = Range("correct").Value

    Range("results").Offset(attempts, 0).Value = attempts + 1
    Range("results").Offset(attempts, 1).Value = lblTopNum.Caption
    Range("results").Offset(attempts, 2).Value = lblBotNum.Caption
    Range("results").Offset(attempts, 3).Value = lblBop.Caption
    Range("results").Offset(attempts, 4).Value = Range("Answer").Value
    Range("results").Offset(attempts, 5).Value = txtAnswer.Text

    If (Range("Answer").Value = txtAnswer.Text) Then
        Range("results").Offset(attempts, 6).Value = 1
    Else
        Range("results").Offset(attempts, 6).Value = 0
    End If

    'Update attempts and success
    Range("attempts").Value = attempts + 1
    Range("correct").Value = correct + 1

    newProblem

End Sub

Sub newProblem()

    Application.Calculate
    formMath.lblTopNum.Caption = Range("TopNum").Value
    formMath.lblBotNum.Caption = Range("BotNum").Value
    formMath.lblBop.Caption = Range("ProbType").Value
    formMath.txtAnswer.Value = ""
    'formMath.txtAnswer.SetFocus

End Sub

2 个答案:

答案 0 :(得分:1)

开始

您可以在设计模式下,将文本框的TabIndex属性设置为0,也可以将焦点设置在UserForm_Initialize()

中的文本框上
Private Sub UserForm_Initialize()
    TextBox1.SetFocus
End Sub

同样,在您执行任何操作后,只需致电TextBox1.SetFocus即可恢复为文本框。

Option Explicit

Private Sub UserForm_Initialize()
    TextBox1.SetFocus
End Sub

Private Sub CommandButton1_Click()
    MsgBox "Hello from Button 1"
    TextBox1.SetFocus
End Sub

Private Sub CommandButton2_Click()
    MsgBox "Hello from Button 2"
    TextBox1.SetFocus
End Sub

如果这不是您想要的,请告诉我?

答案 1 :(得分:1)

我找到了实现这个目标的方法。在上面的代码中,我取出了_KeyPress和_AfterUpdate事件,并将其替换为:

Private Sub txtAnswer_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
        Case 13: recordAnswer
    End Select
End Sub

不确定为什么其他方法不起作用,但确实如此。

也不确定为什么直接设置焦点不起作用。我怀疑焦点是在设置,但随后发生了其他事情,这改变了文本框的焦点。只是一个猜测。

感谢您的帮助。我很感激。

相关问题