Results 1 to 7 of 7

Thread: Listbox : Center the selection line ?

  1. #1

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    272

    Listbox : Center the selection line ?

    Hi friends,

    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 :
    Name:  lisbox_centered.gif
Views: 162
Size:  4.8 KB

    Thanks
    1 Hour vinyl mix live on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Listbox : Center the selection line ?

    What have you tried?
    Sam I am (as well as Confused at times).

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Listbox : Center the selection line ?

    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.

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,997

    Re: Listbox : Center the selection line ?

    Code:
    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
    Attached Files Attached Files

  5. #5
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Listbox : Center the selection line ?

    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.
    Sam I am (as well as Confused at times).

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Listbox : Center the selection line ?

    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.

  7. #7

    Thread Starter
    Hyperactive Member Couin's Avatar
    Join Date
    Dec 2020
    Posts
    272

    Re: Listbox : Center the selection line ?

    Hi friends,

    Thanks for your great help

    @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 on Eurodance90 each sunday 10:00 PM (French Timezone) - New non-official Jingle Palette update Jingle Palette Reloaded

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width