在Excel VBA中禁用列表框垂直滚动条

时间:2014-02-11 02:58:37

标签: excel-vba vba excel

我想知道如何禁用列表框滚动条。因为我已经有一个滚动条可以控制两个文本框同时滚动在一起。据我所知,For Horizo​​ntal只需添加宽度,垂直滚动条如何不改变任何高度值? 这是listbox属性所固有的,但无论如何要改变它?谢谢你的帮助。

图像:

enter image description here

1 个答案:

答案 0 :(得分:0)

没有内置属性可用于隐藏滚动条。

通常API工作,但在这种情况下它不起作用。逻辑是获取列表框的句柄,然后隐藏滚动条。例如

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

Private Declare Function ShowScrollBar Lib "user32" (ByVal hWnd As Long, _
ByVal wBar As Long, ByVal bShow As Long) As Long

Private Const SB_HORZ = 0 '<~~ Horizontal Scrollbar
Private Const SB_VERT = 1 '<~~ Vertical Scrollbbar
Private Const SB_BOTH = 3 '<~~ Both ScrollBars

Dim lngMyHandle As Long, ChildRet As Long
Dim i As Long

Private Sub UserForm_Initialize()
    For i = 1 To 100
        ListBox1.AddItem i
        ListBox2.AddItem i
    Next i
End Sub

Private Sub CommandButton1_Click()
    lngMyHandle = FindWindow("THUNDERDFRAME", Me.Caption)

    If lngMyHandle <> 0 Then Debug.Print "Found Userform's handle"

    ChildRet = FindWindowEx(lngMyHandle, ByVal 0&, "F3 Server 516c0000", vbNullString)

    If ChildRet <> 0 Then Debug.Print "Found Listbox's Handle"

    '~~> I Found the listbox Handle but it REFUSES TO WORK!!!
    ShowScrollBar ChildRet, SB_BOTH, False
End Sub

我使用spy ++来获取列表框的类,如下所示,在上面的代码中我得到ChildRet的值,但我很失望。我第一次难以理解为什么API无效,我将继续尝试。

enter image description here

<强> ALTERNATIVE

说过有另一种选择。将列表框放在单独的框架中,并减小框架的宽度,使其隐藏滚动条。见这个例子

enter image description here

这是我能想到的最简单的方式。