C# ListBox 自动滚动到底部 方法:
在ListBox中添加一条记录(ListBox.Items.Add方法)后,滚动条会自动回到顶部。我们可能更希望它自动滚动到底部,简要介绍几种方法。

  方法一:

 

this.listBox1.Items.Add("new line"); 
this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1; 
this.listBox1.SelectedIndex = -1;


  在添加记录后,先选择最后一条记录,滚动条会自动到底部,再取消选择。

  缺点是需两次设置选中条目,中间可能会出现反色的动画,影响美观。

  方法二:

this.listBox1.Items.Add("new line");
this.listBox1.TopIndex = this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight);


  通过计算ListBox显示的行数,设置TopIndex属性(ListBox中第一个可见项的索引)而达到目的。

  方法二 plus:智能滚动

复制代码

bool scroll = false; 
if(this.listBox1.TopIndex == this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight)) 
scroll = true;
this.listBox1.Items.Add("new line"); 
if (scroll)
this.listBox1.TopIndex = this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight);

复制代码

  在添加新记录前,先计算滚动条是否在底部,从而决定添加后是否自动滚动。

  既可以在需要时实现自动滚动,又不会在频繁添加记录时干扰用户对滚动条的控制。

C# winform ListBox实现滚动条自动滚动至底部

2020-05-10T01:31:47.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ListBoxAutoScroll
{
    public partial class Form1 : Form
    {
        Thread[] threadArr = new Thread[20];
        bool scroll = false;
        object obj = new object();
        delegate void AddItemCallback(string text);
        public Form1()
        {
            InitializeComponent();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            listBoxMsg.Items.Clear();
            for (int i = 0; i < threadArr.Length; i++)
            {
                if (threadArr[i] == null || threadArr[i].ThreadState == ThreadState.Aborted || threadArr[i].ThreadState == ThreadState.Stopped)
                {
                    threadArr[i] = new Thread(new ThreadStart(() => AddData(i)));
                    threadArr[i].Name = i.ToString();
                }
                threadArr[i].Start();
            }
        }
        public void AddData(int i)
        {

            while (true)
            {
                AddItem("ListBox中添加【第" + i.ToString() + "项】");
                Thread.Sleep(1000);
            }
        }
        private void AddItem(string text)
        {
            if (this.listBoxMsg.TopIndex == this.listBoxMsg.Items.Count - (int)(this.listBoxMsg.Height / this.listBoxMsg.ItemHeight))
                scroll = true;
            if (this.listBoxMsg.InvokeRequired)
            {
                AddItemCallback d = new AddItemCallback(AddItem);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.listBoxMsg.Items.Add(text);
            }
            if (scroll)
                this.listBoxMsg.TopIndex = this.listBoxMsg.Items.Count - (int)(this.listBoxMsg.Height / this.listBoxMsg.ItemHeight);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < threadArr.Length; i++)
            {
                if (threadArr[i] != null && threadArr[i].ThreadState != ThreadState.Stopped && threadArr[i].ThreadState != ThreadState.Aborted)
                {
                    threadArr[i].Abort();
                }
            }
        }
    }
}
Last modification:May 10, 2020
如果觉得我的文章对你有用,请随意赞赏