vba 如何根据需要将焦点放在文本框上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10906714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to put focus to textbox as needed
提问by AndroidDev
I have a textbox on a userform. If the user fails to enter anything in this textbox, I need to trap that to force an entry. I can do this easily enough, but after notifying the user tht they need to make an entry, I want the focus to return to the textbox. Right now, it doesn't do that. Here is my code:
我在用户表单上有一个文本框。如果用户未能在此文本框中输入任何内容,我需要捕获它以强制输入。我可以很容易地做到这一点,但在通知用户他们需要输入后,我希望焦点返回到文本框。现在,它不这样做。这是我的代码:
Private Sub txtAnswer_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 13:
If Me.txtAnswer.Value = "" Then
temp = MsgBox("You need to enter an answer!", vbCritical + vbOKOnly, "No Answer Found!")
Me.txtAnswer.SetFocus
Else
recordAnswer
End If
End Select
End Sub
This code works fine in that the message box pops up if the textbox is left blank. After clearing the message box, if I hit enter immediately again, the message box reappears, suggesting that the focus is on the textbox. However, if I try to enter a character (like the number '1' for example) nothing appears in the textbox.
此代码工作正常,因为如果文本框留空,消息框会弹出。清除消息框后,如果我再次立即回车,消息框会重新出现,提示焦点在文本框上。但是,如果我尝试输入一个字符(例如数字“1”),文本框中不会出现任何内容。
Can anybody suggest how I can get the focus back on this textbox in a way that will allow the user to enter data? Thank you!
任何人都可以建议我如何以允许用户输入数据的方式重新关注此文本框?谢谢!
回答by Reafidy
Why are you not using an 'ok' button to complete the action?
为什么不使用“确定”按钮来完成操作?
You should not bother users with messages while they are typing in a form. Do it at the end.
您不应该在用户输入表单时用消息打扰他们。最后做。
Private Sub OK_Click()
'// Validate form
If txtAnswer.Text = vbNullString Then
MsgBox "You need to enter an answer!", vbExclamation, "No Answer Found!"
txtAnswer.SetFocus
Exit Sub
End If
'// You have reached here so form is correct carry on
recordAnswer
End Sub
If you really want to use the behaviour you asked for then try this:
如果您真的想使用您要求的行为,请尝试以下操作:
Private Sub txtAnswer_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 13:
If Me.txtAnswer.Value = "" Then
temp = MsgBox("You need to enter an answer!", vbCritical + vbOKOnly, "No Answer Found!")
KeyCode = 0
Else
recordAnswer
End If
End Select
End Sub
The problem is that in your code you are setting focus but the enter key is firing afterwards. You don't need to set focus because the textbox already has the focus you just need to cancel the enter key.
问题在于,在您的代码中,您正在设置焦点,但随后会触发 Enter 键。您不需要设置焦点,因为文本框已经拥有焦点,您只需要取消回车键即可。
回答by Getting_there
The other answers seem really complicated. I had a similar problem and really wanted a text warning. It seemed easier for me to just make an invisible label on the form that would show up if the input was incorrect. I also made the background of the label red so that the user would notice something was wrong. Doing it this way kept the cursor visible and right where they left off.
其他答案似乎非常复杂。我有一个类似的问题,真的想要一个文本警告。对我来说,在表单上制作一个不可见的标签似乎更容易,如果输入不正确,它就会显示出来。我还将标签的背景设为红色,以便用户注意到有问题。这样做可以使光标保持可见,并且就在他们离开的地方。
Public Function amount(ByRef cont As MSForms.TextBox) As Integer
'makes sure that a number is used
'could change to account for decimals if necessary
Dim i As Long
On Error Resume Next
i = 0
If (cont.Value = "") Then Exit Function
Do While i < 1000000
If (cont.Value = i) Then
UserForm1.Label257.Visible = False
Exit Function
End If
i = i + 1
Loop
UserForm1.Label257.Visible = True
amount = 1
End Function
Public Sub qty_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
If amount(UserForm1.qty) = 1 Then
Cancel = True
End If
End Sub
I hope this helps other who run into this problem later on.
我希望这可以帮助以后遇到这个问题的其他人。
回答by Bryan
Looking at the above code, I assume the i counter is to keep it going? Sorry a bit rusty, been a few years since I've done code.
看看上面的代码,我假设 i 计数器是让它继续运行?抱歉有点生疏,自从我完成代码以来已经有几年了。
At any rate, if thats the case you could always run it while i=0, do (or while true).
无论如何,如果是这种情况,您可以在 i=0 时始终运行它,执行(或在 true 时)。
Sorry, first time posting here, hope that made sense.
对不起,第一次在这里发帖,希望有道理。