Pezzzz Pezzzz - 1 month ago 6
Vb.net Question

How to a scroll a listbox to see the last item added, on event

I have a single line textbox that is used to add numerical strings to a checked listbox. I want the listbox to auto scroll to the last item added if this is not visible to the user. I have looked for scroll properties of the listbox but I can't find anything that looks like it will scroll the listbox.

Does anyone have any advice?

Here is the code that adds an item to the listbox:

Private Sub bttAddchklstDbManagement_Click(sender As System.Object, e As System.EventArgs) Handles bttAddchklstDBmanagement.Click
If Not txtDBManagement.Text = Nothing And Not txtDBManagement.Text = "" Then
chklstDBmanagement.Items.Add(txtDBManagement.Text)
chklstDBmanagement.SetItemChecked(chklstDBmanagement.Items.Count - 1, True)
txtDBManagement.Text = Nothing
txtDBManagement.Focus()
End If
End Sub


txtDBmanagement is the TextBox

chklstDbManagement is the checked listbox

Answer

Use TopIndex after adding the item.

    private void button1_Click(object sender, EventArgs e)
    {
        checkedListBox1.Items.Add("item");
        checkedListBox1.TopIndex = checkedListBox1.Items.Count - 1;
    }