RichTextBox颜色文本问题 [英] RichTextBox Color Text problems

查看:101
本文介绍了RichTextBox颜色文本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先是您可能需要的信息,我正在使用.NET 3.5.

我的问题是,我的RichTextBox不想给textpart上色.我已经问过谷歌并尝试了10种不同的方式.''我还搜索了codeprojekt.这是代码部分:

Hi,

first the information you may require, i''m using .NET 3.5.

My Problem is, that my RichTextBox does not want to color textparts. I already asked google and tried 10 different way''s. I also searched codeprojekt. Here is the Codepart:

this.BeginInvoke(new MethodInvoker(delegate()
            {
                output.SelectionStart = output.Text.Length;
                //output.SelectionFont = font;
                output.SelectionColor = Color.Red;
                output.AppendText(text);
                Application.DoEvents();
            }));



但是文本仍然不是红色-.-



But the Text is still not Red -.-

推荐答案

,您还需要设置SelectionLength.

因此,我认为我不了解您要执行的操作.

您是否要在输出后附加text并突出显示它?如果是这样,您就把代码混在一起了.首先,您添加文本.然后,将SelectionStart设置为文本的开始位置.然后,将SelectionLength设置为添加的文本的长度.然后您更改SelectionColor.
you need to set the SelectionLength as well.

For that matter, I don''t think I understand what you''re trying to do.

Are you trying to append text to the output and then highlight it? If so, you''ve got your code all mixed up. First, you append the text. Then, you set the SelectionStart to where the text begins. Then, you set the SelectionLength to the length of the added text. Then you change the SelectionColor.


您需要做的是注意选择内容的开始位置,附加文本,选择附加的文本,然后 更改所选内容的颜色.
What you need to do is note the position of the start of the selection, append the text, select the appended text and then change the color of the selection.


好,因此,首先,不要发布实际上不是答案的答案.

作为仅供参考,我创建了一个仅带有RichTextBox,Button和BackgroundWorker的表单.这是它的代码:

Ok, so, first, don''t post an answer that isn''t actually an answer.

As an FYI, I created a form with just a RichTextBox, a Button, and a BackgroundWorker. Here''s the code for it:

private void button1_Click_1(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync(richTextBox1);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    RichTextBox caller = (RichTextBox)e.Argument;
    string text = "Test";
    if (caller.InvokeRequired)
    {
        caller.BeginInvoke(new MethodInvoker(delegate()
        {
            int selectionStart = caller.Text.Length;
            caller.AppendText(text);
            caller.SelectionStart = selectionStart;
            caller.SelectionLength = text.Length;
            caller.SelectionColor = Color.Red;
        }));
    }
}



它将字符串"Test"添加到RichTextBox,并使颜色变为红色.因此,如果它不适合您,则说明您做错了.



It adds the string "Test" to the RichTextBox and makes the color red. So if it''s not working for you, you''re doing something wrong.


这篇关于RichTextBox颜色文本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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