在 Excel VBA 中禁用列表框垂直滚动条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21692158/
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
Disable listbox Vertical Scrollbar in Excel VBA
提问by whywhy
i wondering how to disable the listbox scrollbar.Because i already have a scroll bar which can control both textbox scroll together at the same time. As i know,For Horizontal just add the width, how about vertical scroll bar without changing any height value? This is inherent for listbox property,but anyway to change it? Thanks for help.
我想知道如何禁用列表框滚动条。因为我已经有一个滚动条,它可以同时控制两个文本框一起滚动。据我所知,对于水平只是添加宽度,垂直滚动条不改变任何高度值怎么样?这是列表框属性固有的,但无论如何要更改它?感谢帮助。
Image:
图片:
回答by Siddharth Rout
There is no inbuilt property that you can use to hide the scrollbars.
没有可用于隐藏滚动条的内置属性。
Usually the APIs work but in this case it is not working. The logic is to get the handle of the listbox and then hide the scrollbar. For example
通常 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
I used spy++ to get the class of the listbox as shown below and in the code above I do get the value of ChildRet
but I was disappointed. For the first time I am having a difficulty to understand as to why the API's are not working and I will continue experimenting with it.
我使用 spy++ 来获取列表框的类,如下所示,在上面的代码中,我确实得到了 的值,ChildRet
但我很失望。我第一次很难理解为什么 API 不起作用,我将继续试验它。
ALTERNATIVE
选择
Having said that there is an alternative. Place the Listbox in individual frames and reduce the width of the frame so that it hides the scrollbar. See this example
话虽如此,但还有另一种选择。将列表框放在单独的框架中并减小框架的宽度以隐藏滚动条。看这个例子
This is the most simplest way I could think of.
这是我能想到的最简单的方法。