在一定情况下自动滚动列表框 [英] Autoscrolling a Listbox under a certain situation

查看:229
本文介绍了在一定情况下自动滚动列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何加入新的项目后自动滚动列表框,但只有当滚动条是在项目被添加?


解决方案

此示例代码应该帮助你。我这样做有一个TextBox很多次,但它了一段时间来弄明白一个列表框



显然,它只是一个带有一个按钮和一个列表框形式。修改以满足您的需要:

 私人无效的button1_Click(对象发件人,EventArgs五)
{
listBox1中.Items.Add(一些文本+ listBox1.Items.Count.ToString());

//项目的最大数量,该列表框可以一次
INT NumberOfItems = listBox1.ClientSize.Height / listBox1.ItemHeight显示;

如果(listBox1.TopIndex == listBox1.Items.Count - NumberOfItems - 1)
{
//顶部的项目时,你可以看到底部项目
listBox1.TopIndex = listBox1.Items.Count - NumberOfItems + 1;
}
}


How to autoscroll a Listbox after adding a new item, but only if the scrollbar is at the bottom before the item is added?

解决方案

This sample code should help you out. I've done this many times with a TextBox but it took awhile to figure it out for a ListBox

Obviously, it's just a Form with a Button and a ListBox. Modify to fit your needs:

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;
    }
}

这篇关于在一定情况下自动滚动列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆