ListBox VScrollBar ???

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a ListBox on my form and wand to do auto scrolling, I can do that by:
myListBox.ClearSelected();
OutputListBox.SelectedIndex = myListBox.Items.Count - 1;

But it causes to unselect all selected items, so I wish do that by moving
the vertical scroll bar, imbedded in the ListBox.
But I couldn’t find a way to get the ListBox vertical scroll bar.
Can anybody show how to obtain it and how to make move/scroll??
 
Override ListBox class as following :

public class ListBoxScroll:ListBox {
private const int SB_HORZ=0;
private const int SB_VERT=1;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int GetScrollPos(int hWnd,int nBar);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SetScrollPos(int hWnd,int nBar,int nPos,bool
bRedraw);
public int HScrollPos {
get {return GetScrollPos((int)Handle,SB_HORZ);}
set {SetScrollPos((int)Handle,SB_HORZ,value,true);}
}
public int VScrollPos {
get {return GetScrollPos((int)Handle,SB_VERT);}
set {SetScrollPos((int)Handle,SB_VERT,value,true);}
}
}

You have now two new members, called HScrollPos et VScrollPos. The last is
the one you are looking for.

Hope it helps,
Ludovic SOEUR.
 
Thanks Ludovic,
But it does not work; the set does not work as expected.
I do succeed to move the scroll to the bottom of it range but does not move
the items of the ListBox, and off course this is what it’s all for.

Any idea ?
 
Funny control (it keeps in memory the scrollbar location to draw its items).
There is another way to let it believes that the user changed the scrollbar
location, using the WM_VSCROLL message. Use the following lines instead :

public class ListBoxScroll:ListBox {
private const int WM_VSCROLL=0x0115;
private const int SB_VERT=1;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int GetScrollPos(int hWnd,int nBar);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(int hWnd,uint Msg,int wParam,int
lParam);
public int VScrollPos {
get {return GetScrollPos((int)Handle,SB_VERT);}
set
{SendMessage((int)Handle,WM_VSCROLL,((value<0?0:value&255)<<16)+(int)ScrollE
ventType.ThumbPosition,0);}
}
}

If think this time it is what you were looking for.

Hope it helps.

Ludovic SOEUR.
 
Well , I found an easier way:

myListBox.TopIndex = myListBox.Items.Add("the string to add");

This way it does auto scrolling as I want him to.

Thanks
Sharon
 
Back
Top