Before continue digging the web, is it feasable to center the slection line in a listbox that contains enough items ?
I mean, for example, a listbox , of 5 lines height (size).
The list has 10 items.
Cases of selected item 0 or 1 : The items list is at her top , and the selection line is on the first (for item 0) or second (for item 1) place of listbox height.
Cases of selected item between 2 and 8 , the selection line if at the middle of the listbox height (so at the 3rd place), changing the selected item moves the items list, not the selection line.
Cases of slected item 8 or 9 , the list is at her bottom and the selection line if on place 4 and 5 of listbox height.
I built a small gif to show the behavior I would :
Thanks
1 Hour vinyl mix live onEurodance90each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded
It would definitely take some subclassing of the ListBox.
Personally, I'd probably "home-grow" (custom user control) my own ListBox, possibly using a small array of TextBoxes that the items scroll through. Using a small array of TextBoxes, I can certainly see my way through how to do it (no subclassing needed).
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Function GetListBoxItemHeight(nListbox As ListBox) As Long
Const LB_GETITEMHEIGHT = &H1A1
GetListBoxItemHeight = SendMessage(nListbox.hwnd, LB_GETITEMHEIGHT, 0, 0) * Screen.TwipsPerPixelY
End Function
Private Sub List1_Click()
Dim iVisibleItems As Long
Dim iTopIndex As Long
iVisibleItems = List1.Height / GetListBoxItemHeight(List1)
iTopIndex = List1.ListIndex - (iVisibleItems / 2 - 0.1)
If iTopIndex < 0 Then iTopIndex = 0
List1.TopIndex = iTopIndex
End Sub
you mean this doesn't do it (easily)? (Maybe I misunderstood):
Code:
Private Sub List1_Click()
With List1
If .ListIndex < 3 Then .TopIndex = 0
If .ListIndex > 2 And .ListIndex < .ListCount - 1 Then .TopIndex = .ListIndex - 2
End With
End Sub
Of course, I am assuming a listbox showing 5 items, but that can be accounted for in that first IF statement.
Ahhh, I totally misunderstood as well. I thought he wanted the items horizontally centered. Looks like Eduardo got it done though.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
@SamOscarBrown : I tried nothing because I searched (probably with wrong words) but not found any example or even function that was allowing what I would
@Elroy : No worries
@Eduardo : Perfect ! I transposed your example code to the project and looks running nice ! Thanks a lot again !
See ya !
1 Hour vinyl mix live onEurodance90each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded