Richtextbox控件修改帮助 [英] Richtextbox control modification help

查看:75
本文介绍了Richtextbox控件修改帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想加载richtextbox文件时字母颜色对于不同的字母表是不同的。



就像一个是绿色,b是白色,c是红色等。

解决方案

你可以尝试通过EditedControlShowind event改变颜色


可以通过选择RichTextBox中的每个字符,一次一个,并调用YourRichTextBox.SelectionColor = #SomeColor;



使用函数计算或查找每个字符的新颜色#SomeColor。



但是,如果你这样做,一个带有 lot 文本的RichTextBox,即使你使用一些古老技术来减少这样的RichTexBox闪烁,你最终会得到(很可能)一个闪烁的混乱:[ ^ ]。



假设你有一些特殊的目的,你可以采用一种方法来解决这个问题......考虑到速度......就是创建一个字符和颜色的查找表。这假设你总是希望相同的角色是相同的颜色。



你可以创建一个这样的字典:

 私人字典< char,Color> dctCharColor; 

然后在代码中的某处初始化它:

 dctCharColor =  new  Dictionary< char,Color>() ; 
{
{' a',Color.Green},
{' b',Color.White},
{' c',Color.Red}
};

然后,例如,你可以使用以下方法为字符着色:

  private   void  ApplyColor( RichTextBox theRichTextBox)
{
theRichTextBox.SuspendLayout();

for int i = 0 ; i < theRichTextBox.TextLength; i ++)
{
ch = theRichTextBox.Text.ToLower()[i ]。

if (dctCharColor.Keys.Contains(ch))
{
theRichTextBox.Select(i, 1 );
theRichTextBox.SelectionColor = dctCharColor [ch];
}
}

theRichTextBox.ResumeLayout();
}

我会选择在RichTextBox填充其内容后设置字符颜色,因为我想隔离文件读取并分别处理文件读取中的任何错误。


Hi
I want on loading richtextbox file the alphabets color are different for different alphabets.

like a is green, b is white, c is red etc..

解决方案

you can try to change color through "EditedControlShowind event"


You could do this by selecting each character in the RichTextBox, one-at-a-time, and calling YourRichTextBox.SelectionColor = #SomeColor;

Where you vary #SomeColor using a function to calculate, or look-up, a new color for each character.

However, if you do this, in a RichTextBox with a lot of text, what you will end up with is (very likely) a flickering mess, even if you use some "ancient" technique for reducing RichTexBox flicker like this: [^].

Assuming you have some special purpose in mind, one way you might approach this ... with speed in mind ... is to create a look-up table of characters and colors. That's assuming you always want the same character to be the same color.

You could create a Dictionary like this:

private Dictionary<char, Color> dctCharColor;

Then initialize it somewhere in your code:

dctCharColor = new Dictionary<char, Color>();
{
     { 'a', Color.Green },
     { 'b', Color.White},
     { 'c', Color.Red }
};

And then, for example, you could color the characters with a method like this:

private void ApplyColor(RichTextBox theRichTextBox)
{
    theRichTextBox.SuspendLayout();
    
    for (int i = 0; i < theRichTextBox.TextLength; i++)
    {
        ch = theRichTextBox.Text.ToLower()[i];

        if (dctCharColor.Keys.Contains(ch))
        {
            theRichTextBox.Select(i, 1);
            theRichTextBox.SelectionColor = dctCharColor[ch];
        }
    }
    
    theRichTextBox.ResumeLayout();
}

I would make the choice to set the character colors after the RichTextBox was filled with its content because I'd want to "isolate" file reading and handle any errors in file reading separately.


这篇关于Richtextbox控件修改帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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