Center Selection in Listbox

  • Thread starter Thread starter Kris Morrison
  • Start date Start date
K

Kris Morrison

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?

Thanks Kris
 
Kris,

That is not possible, the listbox cannot do anything more than notepad. Have
a look at the RichTextbox if you can solve your problem with that.

Cor
 
is it possible to populate a richtextbox with items and have each item
have its own index per line basis
 
Duh,

Kris I made a mistake and was answering about a textbox because you told
that you were entering textfiles in the listbox.

A listbox is meant more to load a kind of index for different files. A
listbox is of course no notepad as I wrote as it is derived from
listcontrol.

http://msdn.microsoft.com/en-us/library/system.windows.forms.listcontrol(VS.80).aspx

However, the problems stay the same (or even more), however a RichTextbox is
not usable as well in this situation.

Sorry,

Cor
 
Sorry my application load a text file into a listbox and i just want to
know if it is possible to have the selected text stay centered and the
text move up or down as it is when up press the up or down key the
highlighted selection moves up or down. Like a game menu

Thanks
Kris
 
Sorry my application load a text file into a listbox and i just want to
know if it is possible to have the selected text stay centered and the
text move up or down as it is when up press the up or down key the
highlighted selection moves up or down. Like a game menu

Thanks
Kris

Hi Kris,
Though you can create your own formula to calculate proper centering
ratio, as a sample, assuming you have listbox whose width is "240".(It
can be a little more or less). Then place that code to center selected
item in listbox. Keep in mind that hard-coded width value does not
guarantee that the totalwidth value "65" in code is compatible with
other-sized listboxes as stated before.

Dim content As String
Dim tempindex As Integer
content = ListBox1.SelectedItem
tempindex = ListBox1.SelectedIndex
content = content.PadLeft((65 + _
content.Length) \ 2).PadRight(65)
ListBox1.Items.RemoveAt(tempindex)
ListBox1.Items.Insert(tempindex, content)

When you create a listbox whose width is 240, and but the code in a
button_click or elsewhere, selected item will "look like" centered
using padding tecnique.

Hope it helps,

Onur Güzel
 
Sorry my application load a text file into a listbox and i just want to
know if it is possible to have the selected text stay centered and the
text move up or down as it is when up press the up or down key the
highlighted selection moves up or down. Like a game menu

Thanks
Kris

And to move selected item up in listbox, use:
If Not ListBox1.SelectedIndex = 0 Then
Dim content As String = ListBox1.SelectedItem
Dim tempindex As Integer = ListBox1.SelectedIndex
ListBox1.Items.RemoveAt(tempindex)
ListBox1.Items.Insert(tempindex - 1, content)
End If


' and to move down selected item, just use:

If Not ListBox1.SelectedIndex = _
ListBox1.Items.Count - 1 Then
Dim content As String = ListBox1.SelectedItem
Dim tempindex As Integer = ListBox1.SelectedIndex
ListBox1.Items.RemoveAt(tempindex)
ListBox1.Items.Insert(tempindex + 1, content)
End If

Hope that helps,

Onur Güzel
 
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
 
....and to keep the selected item in the middle (as far as possible if more
than one line is visible in the ListBox) when it's scrolled:

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As
KeyEventArgs) Handles ListBox1.KeyDown
Dim nvisible As Integer = ListBox1.Height \ ListBox1.ItemHeight
ListBox1.TopIndex = ListBox1.SelectedIndex - (nvisible \ 2)
End Sub

Andrew
 
Back
Top