VBA Tab Key 将实际 Tab 值放在文本框中,而不是导航到下一个控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2714404/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:35:29  来源:igfitidea点击:

VBA Tab Key putting actual Tab Value in Text Box instead of navigating to next control

vbaexcel-vbaexcel

提问by ray

I have a VBA form (in Excel if that matters) that contains text boxes.

我有一个包含文本框的 VBA 表单(如果重要的话,在 Excel 中)。

On three occasions, I have found myself pressing the tab key to navigate to the next control, but instead an actual TAB is being put in the text box.

在三种情况下,我发现自己按 Tab 键导航到下一个控件,但实际的 TAB 被放置在文本框中。

The form normally acts as it should, but it does concern me that this gremlin is showing up sporadically.

该表格通常按其应有的方式运作,但我确实担心这种小鬼偶尔会出现。

It has shown up on Office 2003 as well as Office 2007 on two different computers.

它已出现在 Office 2003 和 Office 2007 的两台不同计算机上。

Has anyone else encountered this problem and, if so, how did you fix it?

有没有其他人遇到过这个问题,如果有,你是如何解决的?

采纳答案by Tmdean

I was able to reproduce the problem 100% of the time by launching Excel, immediately pulling up the form, and holding down the tab key.

通过启动 Excel,立即拉出表单并按住 Tab 键,我能够 100% 地重现该问题。

If I change any code at all in the form and resave the workbook, the problem goes away. I'm going to chalk this up to a fluke compilation error within VBA.

如果我更改表单中的任何代码并重新保存工作簿,问题就会消失。我将把它归结为 VBA 中的一个侥幸编译错误。

回答by Peter Manchester

As a quick work-around, use this code in the control's Exit event.

作为快速解决方法,在控件的 Exit 事件中使用此代码。

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox1.Text = VBA.Replace(TextBox1.Text, VBA.Chr(9), "")
End Sub

回答by Tony Dallimore

I created a form with three text boxes. I entered characters and tabbed onto the next for some time without being able to duplicate your problem.

我创建了一个包含三个文本框的表单。我输入了字符并在下一个选项卡上切换了一段时间,但无法复制您的问题。

The only way I can get an tab into the text box is by entering Ctrl+Tab. This might be embarrassing but backspace removes it so it is not a major issue. Is it possible that you are accidentally pressing Ctrl at the same time?

将选项卡放入文本框中的唯一方法是输入 Ctrl+Tab。这可能令人尴尬,但退​​格键会将其删除,因此这不是主要问题。是否有可能您不小心同时按下了 Ctrl?

I find occasionally that if I mispress a key that the cursor jumps to somewhere else on the screen. I am not quite sure what I mean by "mispress"; it seems to be something to do with pressing two keys at once. This seems to be a feature of modern keyboards and how they detect which key has been pressed because I have encountered it on many different computers. The implication is that by mispressing a key, a control character (perhaps tab or ctrl+tab) is generated.

我偶尔会发现,如果我按错了一个键,光标就会跳到屏幕上的其他地方。我不太确定我所说的“误压”是什么意思;这似乎与一次按下两个键有关。这似乎是现代键盘的一个特性,以及它们如何检测按下了哪个键,因为我在许多不同的计算机上都遇到过它。这意味着通过误按一个键,会生成一个控制字符(可能是 tab 或 ctrl+tab)。

I also tried the following which worked and conceals the problem by removing the tab and moving on to the next control.

我还尝试了以下操作,通过删除选项卡并转到下一个控件来隐藏问题。

Private Sub TextBox1_Change()

  If InStr(1, TextBox1.Text, Chr(9)) <> 0 Then
    TextBox1.Text = Replace(TextBox1.Text, Chr(9), "")
    TextBox2.SetFocus
  End If

End Sub

回答by Unicco

This might solve the problem:

这可能会解决问题:

Public Sub MoveFocusToNextControl(xfrmFormName As UserForm, _
xctlCurrentControl As control)

Dim xctl As control
Dim lngTab As Long, lngNewTab As Long

On Error Resume Next

' Move focus to the next control in the tab order
lngTab = xctlCurrentControl.TabIndex + 1
    For Each xctl In xfrmFormName.Controls
        lngNewTab = xctl.TabIndex
        ' An error will occur if the control does not have a TabIndex property;
        ' skip over those controls.
        If Err.Number = 0 Then
            If lngNewTab = lngTab Then
                xctl.SetFocus
                Exit For
            End If
        Else
            Err.Clear
        End If
    Next xctl
Set xctl = Nothing
Err.Clear
End Sub

回答by Tomalak

Set the TabKeyBehaviorproperty to Falseto get "Tab jumps to next field" behavior.

将该TabKeyBehavior属性设置为False“Tab 跳转到下一个字段”行为。