更新期间停止闪烁的TextBox [英] Stopping TextBox flicker during update

查看:265
本文介绍了更新期间停止闪烁的TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WinForms应用程序,我使用的日志文件文本框。我使用的附加文本,而不形式闪烁 TextBox.AppendText(字符串); ,但是当我试图清除旧文本(作为控制的。文本属性达到.MaxLength 。限制),我得到可怕的闪烁

My WinForms application has a TextBox that I'm using as a log file. I'm appending text without the form flickering using TextBox.AppendText(string);, however when I try to purge old text (as the control's .Text property reaches the .MaxLength limit), I get awful flicker.

我正在使用的代码如下:

The code I'm using is as follows:

public static void AddTextToConsoleThreadSafe(TextBox textBox, string text)
{
    if (textBox.InvokeRequired)
    {
        textBox.Invoke(new AddTextToConsoleThreadSafeDelegate(AddTextToConsoleThreadSafe), new object[] { textBox, text });
    }
    else
    {
        // Ensure that text is purged from the top of the textbox
        // if the amount of text in the box is approaching the
        // MaxLength property of the control

        if (textBox.Text.Length + text.Length > textBox.MaxLength)
        {
            int cr = textBox.Text.IndexOf("\r\n");
            if (cr > 0)
            {
                textBox.Select(0, cr + 1);
                textBox.SelectedText = string.Empty;
            }
            else
            {
                textBox.Select(0, text.Length);
            }
        }


        // Append the new text, move the caret to the end of the
        // text, and ensure the textbox is scrolled to the bottom

        textBox.AppendText(text);
        textBox.SelectionStart = textBox.Text.Length;
        textBox.ScrollToCaret();
    }
}

是否有从清除文本行的更合适的方法不会引起闪烁控制的顶部?一个TextBox不具有一个ListView具有的BeginUpdate()/ EndUpdate()方法。

Is there a neater way of purging lines of text from the top of the control that doesn't cause flickering? A textbox doesn't have the BeginUpdate()/EndUpdate() methods that a ListView has.

是一个TextBox控件,即使是控制台日志中最适合控制?

Is a TextBox control even the best suited control for a console log?

编辑:文本框闪烁似乎是文本滚动到顶部(当我清除在控制的顶部的文本),然后将其立即滚动回落至底部。 - 这一切发生的非常快,所以我刚才看到重复闪烁

The TextBox flickering appears to be the textbox scrolling up to the top (while I purge the text at the top of the control), and then it immediately scrolls back down to the bottom. - it all happens very quickly, so I just see repeated flickering.

我也刚刚看到的这个问题和建议是使用一个ListBox,但我不知道这是否会工作,我的情况,因为(在大多数情况下)我收到了一次ListBox中的一个字符的文本。

I've also just seen this question, and the suggestion was to use a ListBox, however I don't know if this will work in my situation, as (in most cases) I'm receiving the text for the ListBox one character at a time.

推荐答案

的问题是,你反复并快速增加(删除)一个字符的时间。一个解决办法是将缓冲字符被添加它们,并更新在较大的时间间隔的文本框(无论字符的量),例如,每250毫秒。

The problem is that you are adding (removing) one character at a time repeatedly and quickly. One solution would be to buffer the characters as they are being added and update the textbox at greater intervals (regardless of the amount of characters), for example, every 250 milliseconds.

这将要求:


  • 来有字,其中的数组或堆栈他们会添加

  • 来有一个计时器,将调用的委托,将真正做到与存储在堆栈中的字符,更新

另一种选择是使用每250毫秒和100个字符两者,无论先发生。但是,这可能会代码更没有任何实际的好处复杂化。

Another option is to use both every 250 ms and 100 chars, whatever happens first. But this would probably complicate the code more without any tangible benefit.

这篇关于更新期间停止闪烁的TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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