简体   繁体   English

如何更改richTextBox行文本的一部分?

[英]How can i change part of richTextBox line text?

In download event of my code i have this two lines: 在我的代码的下载事件中,我有这两行:

RichTextBoxExtensions.AppendText(richTextBox1, "Downloading: ", Color.Red);
RichTextBoxExtensions.AppendText(richTextBox1, url, Color.Green);

The part of code 代码的一部分

int count = 0;
        private void DownloadFile()
        {
            if (_downloadUrls.Any())
            {
                WebClient client = new WebClient();
                client.DownloadProgressChanged += client_DownloadProgressChanged;
                client.DownloadFileCompleted += client_DownloadFileCompleted;

                var url = _downloadUrls.Dequeue();

                client.DownloadFileAsync(new Uri(url), @"C:\Temp\New folder (13)\" + count + ".txt");
                RichTextBoxExtensions.AppendText(richTextBox1, "Downloading: ", Color.Red);
                RichTextBoxExtensions.AppendText(richTextBox1, url, Color.Green);
                richTextBox1.AppendText(Environment.NewLine);
                count++;
                countCompleted--;
                label1.Text = countCompleted.ToString();
                return;
            }

            // End of the download
            btnStart.Text = "Download Complete";
        }

        private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                // handle error scenario
                throw e.Error;
            }
            else
            {

            }
            if (e.Cancelled)
            {
                // handle cancelled scenario
            }
            DownloadFile();
        }

What i want to do is each time a file download completed that's in the completed event to change the part "Downloading: " to "Downloaded: " and the whole line. 我想要做的是每次文件下载完成时,在完成的事件中将“下载:”部分更改为“已下载:”和整行。

The class of RichTextBoxExtensions RichTextBoxExtensions类

public class RichTextBoxExtensions
        {
            public static void AppendText(RichTextBox box, string text, Color color)
            {
                box.SelectionStart = box.TextLength;
                box.SelectionLength = 0;

                box.SelectionColor = color;
                box.AppendText(text);
                box.SelectionColor = box.ForeColor;
            }
        }

Here is what you asked: 这是你问的问题:

public static class RichTextBoxExtensions
{
    public static void AppendText(this RichTextBox box, string text, Color color)
    {
        box.SelectionStart = box.TextLength;
        box.SelectionLength = 0;

        box.SelectionColor = color;
        box.AppendText(text);
        box.SelectionColor = box.ForeColor;
    }
    public static void UpdateText(this RichTextBox box, string find, string replace, Color? color)
    {
        box.SelectionStart = box.Find(find, RichTextBoxFinds.Reverse);
        box.SelectionLength = find.Length;
        box.SelectionColor = color ?? box.SelectionColor;
        box.SelectedText = replace;
    }
}

I've also corrected your extension methods, so you can call them directly: 我还更正了您的扩展方法,因此您可以直接调用它们:

richTextBox1.AppendText("Downloading: ", Color.Red);
richTextBox1.AppendText("qwe", Color.Green);

// ...

richTextBox1.UpdateText("Downloading: ", "Downloaded: ", Color.Green);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM