【问题标题】:Get two VBA textboxes to scroll together让两个 VBA 文本框一起滚动
【发布时间】:2019-06-26 11:57:25
【问题描述】:

我有一个宏,可以生成两个多行相关数据的文本框(有时长达数百行)。这些框始终具有相同数量的文本行,每一行对应于另一个 TextBox 中的相邻行。我研究了使用两列 ListBox,但决定使用 TextBoxes,以便用户可以根据需要复制/突出显示/选择数据。

我想这样做,如果用户向下滚动,两个 TextBoxes 一起滚动(即,行保持同步)。

【问题讨论】:

标签: excel vba textbox scrollbar userform


【解决方案1】:

经过大量挖掘和试验,我想通了!通过添加 ScrollBar,我可以使用 ScrollBar_Change() 事件来调整文本框。在我的表单上,我现在有两个 TextBoxes 和一个 ScrollBar 对象。然后我的用户表单代码中有一些必要的子项:

'This constant affects whether the ScrollBar appears or _
   not, as well as some of the movement graphics of the _
   ScrollBar.
'This MUST be reset if the TextBoxes are resized
'I made it a UserForm-level Const because I use it _
   elsewhere, but it could also live in SetUpScrollBar
Private Const TEXTBOX_MAX_LINES_IN_VIEW as Long = 21

Private Sub SetUpScrollBar()
'I call this whenever I show my Userform (happens after a _
   separate macro determines what to put in the TextBoxes). _
   It determines whether the ScrollBar should be shown, and _
   if so, sets the .Max property so it scrolls in accordance _
   to the number of lines in the TextBoxes.

    Dim linesInTextBox as Long

    With Me.TextBox1
        .SetFocus
        linesInTextBox = .LineCount - 1 
        'Need to subtract 1 or you'll get an error _
           when dragging the scroll bar all the way down.
    End With

    'If there are fewer lines than the max viewing area, hide the scroll bar.
    Select Case linesInTextBox > TEXTBOX_MAX_LINES_IN_VIEW
    Case is = True
        ShowScrollBar True
        With Me.ScrollBox
            .Min = 0 'I believe this is the default, but I set it just in case
            .Max = maxLinesInTextBox
            .Value = 0
        End With
    Case is = False
        ShowScrollBar False
    End Select
End Sub

Private Sub ShowScrollBar(show As Boolean)
'A simple way of showing or hiding the scrollbar
    With Me.ScrollBar1
        .Enabled = show
        .Visible = show
    End With
End Sub

Private Sub ScrollBar1_Change()
'When the scrollbar position changes (either by dragging _
   the slider or by clicking it), set the CurLine property _
   of each TextBox to the current ScrollBar value.

    With Me.TextBox1
        'Need to set focus to the box to get or adjust the CurLine property
        .SetFocus
        .CurLine = Me.ScrollBar1.value
    End With

    With Me.TextBox2
        'Need to set focus to the box to get or adjust the CurLine property
        .SetFocus
        .CurLine = Me.ScrollBar1.value
    End With 
End Sub



这似乎对我的目的非常有效。它使我能够在保持数据同步的同时保持使用 TextBox 的文本选择/复制优势。

一些我还没有解决的问题:

  • 滚动工作正常,但如果您尝试单击箭头(尤其是在您刚刚滚动的相反方向),您必须单击直到您的光标到达文本框的顶部。对我来说,这是 21 次点击。有点烦人,但我相信有一个解决方法。
  • 滚动不像普通滚动条那样实时。这意味着您可以拖动滚动条,但在您松开之前它不会更新文本框。
  • 如果用户单击文本框并开始使用箭头键导航,这两个框将变得不同步。下次用户单击 ScrollBar 时,它们将重新同步。如果用户尝试选择的行数多于窗口中可见的行数,这将是非常有问题的:一个 TextBox 将在拖动选择时滚动,而另一个 TextBox 保持原位

【讨论】:

    猜你喜欢
    • 2016-02-15
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 2011-01-22
    • 1970-01-01
    相关资源
    最近更新 更多