Scrolling a list box programmatically

  • Thread starter Thread starter Derrick Repep
  • Start date Start date
D

Derrick Repep

Hi,

I have a VB.NET Windows form that contains a list box. As events occur, I
am adding text to the list box to provide a status update to the user (and
so the user can scroll up to see what occurred). The issue I am having is
that I want the latest status updates (the ones at the bottom of the list)
to be contained within the list box's viewable area as they are added.

Assume the list box is sized so that it can show three lines of text. Given
ten items in the collection, I would like to have items with (zero-based)
indices 7, 8 and 9 shown; when the eleventh item is added, I would like to
show (zero-based) indices 8, 9 and 10.

The addition of items to the collection happens in one place, so there can
be wrapper code to cause this to happen if necessary. Of course, a property
setting would be ideal, but I'll take what I can get. ;-)

Any suggestions? Thanks in advance!

Derrick
 
Hey all,

I found the solution to this issue. I set the TopIndex property of the list
box to be the latest item; the list box will "back fill" the contents of the
viewable area so that it's as full as can be. The single line of code to
accomplish what I wanted to do is

myListBox.TopIndex = myListBox.Items.Count - 1

Regards,
Derrick
 
* "Derrick Repep said:
I have a VB.NET Windows form that contains a list box. As events occur, I
am adding text to the list box to provide a status update to the user (and
so the user can scroll up to see what occurred). The issue I am having is
that I want the latest status updates (the ones at the bottom of the list)
to be contained within the list box's viewable area as they are added.

Assume the list box is sized so that it can show three lines of text. Given
ten items in the collection, I would like to have items with (zero-based)
indices 7, 8 and 9 shown; when the eleventh item is added, I would like to
show (zero-based) indices 8, 9 and 10.

The addition of items to the collection happens in one place, so there can
be wrapper code to cause this to happen if necessary. Of course, a property
setting would be ideal, but I'll take what I can get. ;-)

Have a look at the listbox's 'TopIndex' property.
 
One way to do this is through PInvoke.

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);

private const uint WM_VSCROLL = 0x0115;
private const uint SB_BOTTOM = 0x0007;

private void button1_Click(object sender, System.EventArgs e)
{
SendMessage(this.richTextBox1.Handle, WM_VSCROLL, SB_BOTTOM, 0);
}
 
Back
Top