Kris said:
Hi I have written a program that loads a text file into a listbox and
i want to be able to have the selection centered and the text move up
or down is that possible?
I think everyone else is thinking centred horizontally; I'm guessing you
mean centred vertically.
I put a default ListBox from the toolbox on an empty form, then
double-clicked the empty space on the form to enter this code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' make up something to put in the ListBox
Dim items As New List(Of String)
For i As Integer = AscW("A") To AscW("Z")
items.Add(ChrW(i))
Next
ListBox1.Items.AddRange(items.ToArray)
' we want the selected item to be the middle one
' N.B. use integer division
ListBox1.SelectedIndex = ListBox1.Items.Count \ 2
' how many items are visible in the listbox?
Dim nvisible As Integer = ListBox1.Height \ ListBox1.ItemHeight
' set it so the selected item is centred vertically
ListBox1.TopIndex = ListBox1.Items.Count \ 2 - (nvisible \ 2)
End Sub
Scrollbars will be added automatically with the default ListBox.
Andrew