Changing string colour

为君一笑 提交于 2019-12-04 06:46:48

问题


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;


    }

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.

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.

Note, I am using RichTextBoxes

C# Language!


回答1:


To build on Oliver Jacot-Descombes answer:

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;
        }
    }
}

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.




回答2:


You can use the SelectionColor property of the RichTextBox. Start by selecting the word that you want to format

string word = "white";
int start = richTextBox1.Find(word);
if (start >= 0) {
    richTextBox1.Select(start, word.Length);
    richTextBox1.SelectionColor = Color.Red;
}



回答3:


Add a the reference to the KVP in the dictionary to the Textbox's Tag property. When the user changes the color, get the KVP from the Tag property and change the value in the KVP. I provide insight to the Tag property on my blog C# Winforms and the Hidden Association Tag. WPF/Silverlight also use the Tag property on controls as well..

--- Edit per user request ---

I am not sure why you need to enumerate a dictionary. The whole point of a dictionary is to quickly get the key. My example uses that and does not do the for loop.

... The initialization location....

var myDictionary = new Dictionary<string, Tuple<string, System.Drawing.Color>>()
{
        { "Black", new Tuple<string,System.Drawing.Color>("White", Color.Green) },
        { "White", new Tuple<string,System.Drawing.Color>("Black", Color.Red) }

};

... (later in code)...

private void btnTrans_Click(object sender, EventArgs e)
{

var key = inputBx.Text; // Let us say "White"

if (myDictionary.ContainsKey(key))
{

    var targetColor = myDictionary[key]; // Just get the value

    outputBx.Select(0, targetColor.Item1.Length);
    outputBx.SelectionColor = targetColor.Item2;
    outputBx.Text = targetColor.Item1;
}
else
{
    outputBx.Text = "Unknown";
}

}

Check out my blog article on dictionaries for more info C# Dictionary Tricks



来源:https://stackoverflow.com/questions/9926912/changing-string-colour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!