在特定情况下自动滚动列表框

9 c# scroll listbox

如何在添加新项目后自动滚动列表框,但前提是在添加项目之前滚动条位于底部?

scr*_*789 10

你可以尝试一下:

listBox1.SelectedIndex = listBox1.Items.Count - 1;    
listBox1.SelectedIndex = -1;
Run Code Online (Sandbox Code Playgroud)


col*_*ium 9

此示例代码可以帮助您.我已经使用TextBox多次完成了这项工作,但是需要一段时间才能找到一个ListBox

显然,它只是一个带有Button和ListBox的Form.修改以满足您的需求:

private void button1_Click(object sender, EventArgs e)
{
    listBox1.Items.Add("Some Text " + listBox1.Items.Count.ToString());

    //The max number of items that the listbox can display at a time
    int NumberOfItems = listBox1.ClientSize.Height / listBox1.ItemHeight;

    if (listBox1.TopIndex == listBox1.Items.Count - NumberOfItems - 1)
    {
        //The item at the top when you can just see the bottom item
        listBox1.TopIndex = listBox1.Items.Count - NumberOfItems + 1;
    }
}
Run Code Online (Sandbox Code Playgroud)