0

Good morning,

I would like to implement a system to search for an item in a list. I made the code that this requires but I arrive at a problem.

The text of my label, for example, is this one:

Hipopotamus

I have a string that contains this:

popota

I would like my label to be "Hi" in black, "popota" in red and finally "mus" in black.

I searched a lot of things on the internet, really for a while, so I found a forum for that.

I hope you could help me :)

depasa
  • 25
  • 4
  • 1
    Winforms, Wpf, Xamarin? – Dmitry Bychenko Jan 06 '20 at 19:12
  • If using winforms, I don't believe a label control supports multi-colored text, but [you could use a `RichTextBox` control](https://stackoverflow.com/questions/13220856/how-to-use-multi-color-in-richtextbox) and make it look like a label. Otherwise, please specify the project type so we know specifically which class you're referring to when you say `Label`. – Rufus L Jan 06 '20 at 19:14
  • Also what have you tried for this? There is a lot of material about this -> WPF -> https://stackoverflow.com/questions/751741/wpf-textblock-highlight-certain-parts-based-on-search-condition – panoskarajohn Jan 06 '20 at 19:14
  • Maybe this is what you are looking for: https://stackoverflow.com/questions/275836/multiple-colors-in-a-c-sharp-net-label – Jasc24 Jan 06 '20 at 19:14
  • I'm of course winform. I'll try the richtextbox, thanks for the idea. – depasa Jan 06 '20 at 19:15
  • This shows you how: https://stackoverflow.com/questions/13220856/how-to-use-multi-color-in-richtextbox – Rufus L Jan 06 '20 at 19:16

1 Answers1

0

The Label control doesn't natively support multiple colors, but the RichTextBox control does! And you can set it's properties to look like a label.

For example, to make it look like a label:

private void Form1_Load(object sender, EventArgs e)
{
    // Make the RichTextBox look and behave like a Label control
    richTextBox1.BorderStyle = BorderStyle.None;
    richTextBox1.BackColor = System.Drawing.SystemColors.Control;
    richTextBox1.ReadOnly = true;
    richTextBox1.Text = "Hipopotamus";

    // I added a small, blank Label control to the form which I use to capture the Focus
    // from this control, so the user can't see the caret or select/highlight/edit text
    richTextBox1.GotFocus += (s, ea) => { lblHidden.Focus(); };
}

Then a method to highlight a search term by setting selection start and length and changing selected colors:

private void HighlightSearchText(string searchText, RichTextBox control)
{
    // Make all text black first
    control.SelectionStart = 0;
    control.SelectionLength = control.Text.Length;
    control.SelectionColor = System.Drawing.SystemColors.ControlText;

    // Return if search text isn't found
    var selStart = control.Text.IndexOf(searchText);
    if (selStart < 0 || searchText.Length == 0) return;

    // Otherwise, highlight the search text
    control.SelectionStart = selStart;
    control.SelectionLength = searchText.Length;
    control.SelectionColor = Color.Red;
}

For a test and to show usage, I added a txtSearch textbox control to the form and call the method above in the TextChanged event. Run the form, and type into the textbox to see the results:

private void txtSearch_TextChanged(object sender, EventArgs e)
{
    HighlightSearchText(txtSearch.Text, richTextBox1);
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • Thank you thank you thank you thank you thank you thank you and again thank you for this very detailed answer which works marvellously! It's rare to get responses like this :)))))))))))))) – depasa Jan 06 '20 at 20:11