问题
I've been doing a ton of research and I'm finally giving in and hoping the community can help. I have a FORM CONTROL list box, not an active x. I'm intentionally using form control for a variety of reasons that are not necessarily important (unless you can guide me how to 100% ensure activeX controls don't resize themselves on workbook open). I cannot, for the life of me, figure out how to get the form control list box to scroll to the selected value and put it in view. Here is what I have:
ActiveSheet.ListBoxes("List Box 13").Selected = 100
The list has 1000 values in it to be selected. When this runs, #100 is selected, but is not in view. The list box does not move.
How can I get the scrolling to occur so the selected value is at the top?
ActiveX would be:
.TopIndex
But that is not available for form controls.
回答1:
Scroll to given listbox index to be displayed on top of visible list
I cannot, for the life of me, figure out how to get the form control list box to scroll to the selected value and put it in view.
Seems that actually there's no standard method (leaving aside API calls) to control the visibility of the listbox interior display for a form control list box as opposed to a Userform's ActiveX control list box.
Additional hints
For the sake of completeness (and due to comment) I demonstrate how to scroll into view in a Userform's listbox by combining both navigation properties - i.e. by setting .ListIndex
as well as .TopIndex
together with a defined index value :-), e.g. via a procedure as follows:
Example calls
ScrollTo 100 ' attention: zerobased
or
ScrollTo ActiveSheet.ListBoxes("List Box 13").Selected
Helper procedure
... sets the current ListIndex
to e.g. 100 AND defines it as .TopIndex
.
(Side note: of course targets at the very list end could be displayed even below .TopIndex
depending on the actual .ListCount
)
Private Sub ScrollTo(ByVal idx&)
' Purpose: Scroll to given target index to be displayed on top of visible list
' Site: https://stackoverflow.com/questions/56813550/listbox-form-control-scrolling
' Author: https://stackoverflow.com/users/6460297/t-m
Me.ListBox1.TopIndex = idx
Me.ListBox1.ListIndex = idx
End Sub
来源:https://stackoverflow.com/questions/56813550/listbox-form-control-scrolling