簡體   English   中英

如何通過char在RichTextBox char上顯示文本?

[英]How to display a text on a RichTextBox char by char?

我的窗體上有Form1,RichTextBox1和Button1

了解我要做什么; 看一下此鏈接 ,輸入一個Facebook個人資料鏈接,然后單擊Hack帳戶,然后查看出現的綠色文字

我在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());

        }
    }

但是它不起作用,文本一次出現在文本框中。 不是一個字符一個字符!

您的代碼一次顯示所有文本的原因是使用Thread.Sleep()保持主線程(UI線程)處於掛起/睡眠模式,因此在窗體上未處理任何Application消息,而在窗體上繪制/繪制事件UI線程正在休眠/掛起,因此無法執行工作!


解決方案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;
    }




}

這是因為在您仍在運行代碼時,文本框沒有刷新。 嘗試把這個:

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

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

richTextBox1.Refresh();
        }

您遇到的問題不是附加文本,

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

可以按預期工作,但是問題在於您正在使UI線程進入睡眠狀態,從而阻止了RTF文本框上的文本繪制。

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,而不會阻塞主線程,並保持前端的可用性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM