改变串色 [英] Changing string colour

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

问题描述

好了,所以这是从我的最后一个问题进,但我的代码:

Okay, so this is a carry on from my last question, but I have the code:

private void btnTrans_Click(object sender, EventArgs e)     
    {
        var abrvStr = inputBx.Text;

        foreach (var kvp in d)

        {
            abrvStr = abrvStr.Replace(kvp.Key, kvp.Value);

        }

        outputBx.Text = abrvStr;


    }



基本上它是一本字典计划的一部分,所以,当你在文本框2从dicitonary文本框换成1字输入一行文本中出现1的文本。
所以,如果黑,白是在词典和I输入的壁是黑色的。墙是白色的会出现在文本框2.因此,所有的好。

Basically it's part of a dictionary program, so that when you enter a line of text in textbox 1 it appears in textbox 2 with a word replaced from textbox 1 in the dicitonary. So if black,white is in the dictionary and I enter The wall is black. The wall is white will appear in textbox 2. So all's good.

眼下棘手的部分,我将如何改变这个,让我在文本框2为红色更改字。所以在我上面的例子中,壁白色。怀特将是文本的输出线红色。

Right now the tricky part, how would I alter that to allow me have the changed word in textbox 2 as the colour red. So in my above example, the wall is white. White would be red in the output line of text.

请注意,我用的RichTextBoxes

Note, I am using RichTextBoxes

C#语言!

推荐答案

要建立在奥利弗Jacot-Descombes答案

private void btnTrans_Click(object sender, EventArgs e)     
{
    var abrvStr = inputBx.Text;

    foreach (var kvp in d)

    {            
        abrvStr = abrvStr.Replace(kvp.Key, kvp.Value);
        int start = abrvStr.IndexOf(kvp.Value);
        if(start >= 0) 
        {
            richTextBox1.Text = abrvStr;
            richTextBox1.Select(start, kvp.Value.Length);
            richTextBox1.SelectionColor = Color.Red;
        }
    }
}

您将使用switch语句在字典的价值得到你想要更改选择什么颜色。您将需要修改以适应你的字典中的值,以及你想要什么颜色。

You would use a switch statement on the value of the dictionary to get what color you want to change the selection. You will need to modify that to fit the values in your dictionary as well as what colors you want.

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

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