Middle Sitting Listbox

james2k2
10-06-2009, 01:47 PM
This has probably been asked before, but I personally couldn't find an answer.

I'm after a way to get a listbox to keep the selected item in the middle, but not just that, have it start and end in the middle too, the diagram below should show what i mean. The stars indicate the selected item.

Start Position
--------
| |
| |
| *A* |
| B |
| C |
--------

'B' Position
--------
| |
| A |
| *B* |
| C |
| |
--------

'C' Position
--------
| A |
| B |
| *C* |
| |
| |
--------

Cerian Knight
10-07-2009, 12:06 PM
Maybe this will help:

'Add a listbox tall enough to just show 5 items
Private Sub Form_Load()
List1.AddItem "" 'padding
List1.AddItem "" 'padding
List1.AddItem "A"
List1.AddItem "B"
List1.AddItem "C"
List1.AddItem "D"
List1.AddItem "E"
List1.AddItem "F"
List1.AddItem "" 'padding
List1.AddItem "" 'padding
List1.ListIndex = 2
End Sub

Private Sub List1_Click()
Static lngLastIndex As Long
If Len(List1.Text) Then
List1.TopIndex = List1.ListIndex - 2
Else
List1.ListIndex = lngLastIndex
End If
lngLastIndex = List1.ListIndex
End Sub

EmptyVessel
10-07-2009, 05:55 PM
I think the following makes a cleaner scroll and looks nicer.
As a bonus the height of the scrollbar and picturebox does not have to be dialed in (See Attached Pic)

Personally I'd rewrite this as a user control.


Option Explicit

' Uses:
' Picture1
' Label1 (List of Chars) 'Create inside Picture1
' Label2 (Color Bar (Highligher)) 'Create inside Picture1
' VScrollbar1

' You will have to adjust label2.height so that it is the same height as one row in label1 list.

Const MaxItems As Integer = 5

Private Sub Form_Load()
Dim sList As String
Dim i As Integer

VScroll1.Max = MaxItems
For i = 0 To MaxItems
sList = sList & Chr(i + 65) & vbCrLf
Next i

Label1.BorderStyle = 0 'No border
Label2.BorderStyle = 0

Label2.Caption = " A"

Label1.Left = 0
Label1.Top = Label2.Top

Label1.Caption = sList

Picture1.BackColor = vbWhite
Label1.BackColor = vbWhite
Label2.BackColor = vbCyan
End Sub

Private Sub VScroll1_Change()
Label1.Top = Label2.Top - VScroll1.Value * Label2.Height
Label2.Caption = " " & Chr(65 + VScroll1.Value)
End Sub

james2k2
10-09-2009, 03:56 AM
Many thanks for both ideas so far. I tried the padding idea, but it still doesn't quite do what i'm needing i'm afraid. The second idea is closer to what i'm after but doesn't seem to work too well for scaling. My application is a full screen GUI to interface to a command line application, it will need to be able to resize to fit different screen resolutions. I suppose with some extra code the second idea could be improved as i also need it without the scroll bar.

If you two or anybody else have any further suggestions that would be brilliant. And PS thanks for all the code both of you wrote, much appreciated.

james2k2
10-09-2009, 05:30 AM
Just wanted to tell you all that I decided to expand on the code given by EmptyVessel and have built it into a user control with a more customised list and kinectic-type scrolling. It's looking great now so many thanks for the starting point. Once i've finished adding runtime properties i shall upload it here for all to see.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum