Dana Yeger Dana Yeger - 3 months ago 17
C# Question

List box update with focus on the last line in the listbox

in my application i am add all my new item to my list box, the speed is very fast and i want the list box show me the last item all the time ongoing, i mean that the focus in the list box will be in the last line all the time, how can i do it ?

in this function i am update my list box:

private void bgWSniffer_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
tshark = e.UserState as Tshark;
listBoxPacketsSnifferTab.Items.Add(tshark._packet);
}

Answer

After adding the item to the list box, add this line:

listBoxPacketsSnifferTab.SelectedIndex = listBoxPacketsSnifferTab.Items.Count - 1;

It will make the selected item the last. Keep in mind that the index is zero based.

Read more here:

http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindex.aspx

Comments