使用定时器控制避免标签闪烁

本文关键字:标签 闪烁 控制 定时器 | 更新日期: 2023-09-27 18:22:33

im正在使用代码编辑器(winforms)

我正在使用一个类似于的标签进行计数

http://oi42.tinypic.com/iypoub.jpg

使用此代码:

private void timer_countline_Tick(object sender, EventArgs e)
        {
            updateNumberLabel();
        }
private void updateNumberLabel()
        {
            //we get index of first visible char and number of first visible line
            Point pos = new Point(0, 0);
            int firstIndex = rtb.GetCharIndexFromPosition(pos);
            int firstLine = rtb.GetLineFromCharIndex(firstIndex);
            //now we get index of last visible char and number of last visible line
            pos.X = ClientRectangle.Width;
            pos.Y = ClientRectangle.Height;
            int lastIndex = rtb.GetCharIndexFromPosition(pos);
            int lastLine = rtb.GetLineFromCharIndex(lastIndex);
            //this is point position of last visible char, we'll use its Y value for calculating numberLabel size
            pos = rtb.GetPositionFromCharIndex(lastIndex);

            //finally, renumber label
            numberLabel.Text = "";
            for (int i = firstLine; i <= lastLine + 1; i++)
            {
                numberLabel.Text += i + 1 + "'n";
            }
        }

计时器将间隔设置为1。label dock=左。现在的问题是,每次我运行程序时,标签都会不停地快速闪烁。即使我改变间隔仍然是一样的事情。

但当我将updateNumberLabel()转移到textchange事件时,每次添加richtextbox上的char或我按空格键。

像这样:http://oi40.tinypic.com/a43gcy.jpg

现在我的问题是如何避免这种情况?或者我能做些什么来避免眨眼整个标签何时更新?

提前感谢您的帮助!

使用定时器控制避免标签闪烁

您可能会受益于在标签级别或表单级别启用双缓冲。

这里有两个例子:winforms标签闪烁。