如何通过char在RichTextBox char上显示文本?

时间:2013-01-26 12:19:57

标签: c# .net winforms

我的表单上有Form1,RichTextBox1和Button1

了解我正在做什么;看看this link,输入Facebook个人资料链接,然后点击黑客帐户并查看出现的绿色文字

我在C#中使用下面的代码来实现我想要做的事情:

private void button1_Click(object sender, EventArgs e)
    {
        string myStr = "This is a test string to stylize your RichTextBox1";

        foreach (char c in myStr.ToCharArray()) {

            Thread.Sleep(100);
            richTextBox1.AppendText(c.ToString());

        }
    }

但它不起作用,文本同时出现在文本框中; char不是char!

3 个答案:

答案 0 :(得分:1)

您的代码一次显示所有文本的原因是使用Thread.Sleep()保持主线程(UI线程)挂起/睡眠模式,因此没有任何应用程序消息在表单和处理器上处理由于UI线程处于休眠/暂停状态,因此表单paint / drawing事件无法正常工作!


解决方案1:使用帮助程序线程,以便Thread.Sleep()不会使您的应用程序进入非响应模式

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        string myStr = "This is a test string to stylize your RichTextBox1";

        ThreadPool.QueueUserWorkItem(ShowTextInInterval, myStr);
    }

    private void ShowTextInInterval(object state)
    {
        string mystr = state as string;
        if (mystr == null)
        {
            return;
        }
        for (int i = 0; i < mystr.Length; i++)
        {
            AppendNewTextToRichTextBox(mystr[i]);
            Thread.Sleep(100);
        }
    }

    private delegate void app_char(char c);
    private void AppendNewTextToRichTextBox(char c)
    {
        if (InvokeRequired)
        {
            Invoke(new app_char(AppendNewTextToRichTextBox), c);
        }
        else
        {
            richTextBox1.AppendText(c.ToString(CultureInfo.InvariantCulture));
        }
    }



}

解决方案#2:使用计时器

public partial class Form1 : Form
{
    private Timer tbTimer = new Timer();
    string myStr = "This is a test string to stylize your RichTextBox1";
    private int charPos = 0;
    public Form1()
    {
        InitializeComponent();
        tbTimer.Interval = 100;
        tbTimer.Tick += TbTimerOnTick;

    }

    private void TbTimerOnTick(object sender, EventArgs eventArgs)
    {
        if (charPos < myStr.Length - 1)
        {
            richTextBox1.AppendText(myStr[charPos++].ToString(CultureInfo.InvariantCulture));
        }
        else
        {
            tbTimer.Enabled = false;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        tbTimer.Enabled = true;
    }




}

答案 1 :(得分:0)

这是因为在您仍在运行代码时,文本框不会刷新。试着把它放在:

            foreach (char c in myStr.ToCharArray()) {

            Thread.Sleep(100);
            richTextBox1.AppendText(c.ToString());

richTextBox1.Refresh();
        }

答案 2 :(得分:0)

您遇到的问题不是附加文字

richTextBox1.AppendText(c.ToString()); //Good

按预期工作,但问题是您正在将UI线程置于睡眠状态,阻止在富文本框上绘制文本。

Thread.Sleep(100); //Not so good

快速解决方法是添加

richTextBox1.Refresh();

这会强制在附加文本后重新绘制控件,但请注意,当线程处于休眠状态时,整个UI仍将被冻结。更好的解决方案可能是使用System.Windows.Forms.Timer来实现您的目标。此类每隔指定的时间间隔触发一次事件。一些快速代码,

    private System.Windows.Forms.Timer TextUpdateTimer = new System.Windows.Forms.Timer();
    private string MyString = "This is a test string to stylize your RichTextBox1";
    private int TextUpdateCount = 0;

    private void button1_Click(object sender, EventArgs e)
    {
        //Sets the interval for firing the "Timer.Tick" Event
        TextUpdateTimer.Interval = 100;
        TextUpdateTimer.Tick += new EventHandler(TextUpdateTimer_Tick);
        TextUpdateCount = 0;
        TextUpdateTimer.Start();
    }

    private void TextUpdateTimer_Tick(object sender, EventArgs e)
    {
        //Stop timer if reached the end of the string
        if(TextUpdateCount == MyString.Length) {
            TextUpdateTimer.Stop();
            return;
        }
        //AppendText method should work as expected
        richTextBox1.AppendText(MyString[TextUpdateCount].ToString());
        TextUpdateCount++;
    }

这会在不阻塞主线程的情况下通过char更新文本框char,并保持前端的可用性。