How to scroll at the last item in the listbox through an event

  • Thread starter Thread starter GG
  • Start date Start date
G

GG

I know I can scroll to the last item in the listbox by calling
SetSelected all the time
What I need is when I add items through the Items.Add or Items.AddRange
to be called automatically and go the the bottom.
Anybody knows how to be implemented?

Thanks
 
Well, there isn't an ItemsAdded event on the listbox, but you could build
one if you wanted.

This is just a thought, but does adding an item trigger a resize? If so,
then you could add the logic to select the last item at the resize event.

-James
 
Added the code to SizeChanged and works like a charm
if (lstMsgs.Items.Count==0)
return;
//This selects and highlights the last line
this.lstMsgs.SetSelected(lstMsgs.Items.Count - 1, true);
//This unhighlights the last line
this.lstMsgs.SetSelected(lstMsgs.Items.Count - 1, false);

This is the code in case somebody needs it


Thanks
 
Will take it back. It does not work.

I ended up having a separate proc like this
private void lstMsgsItemsAddScrollBottom(object item)
{
this.lstMsgs.Items.Add(item);
//This selects and highlights the last line
this.lstMsgs.SetSelected(lstMsgs.Items.Count - 1, true);
//This unhighlights the last line
this.lstMsgs.SetSelected(lstMsgs.Items.Count - 1, false);
}
and call this one instead of items.add
 
Back
Top