Click here to Skip to main content
13,053,026 members (60,165 online)
Rate this:
Please Sign up or sign in to vote.
See more:
What I have is a text that I will be reading from a .txt file and certain words that appear in that text should be colored differently (it's like searching for words with a certain error rate)
if the user wanted to find AAA
in text:

then I want ABA to be colored for example red because it has error rate =1
AAA will be colored green because it has error rate of 0
and GHA colored in blue for instance

The text and words are unknown until defined by the user

and I would also would like to have like check boxes with the colors of error rate so it the user checked red and green only word with error rate 0 and 1 will be colored in the text and the rest will be in black ,, I want thid to be dynamically

I hope it's clear thank you

I'm using ASP.NET and C#
Posted 15-Apr-13 9:26am
joshrduncan2012 15-Apr-13 15:31pm
Read each character and append it to a string until you get the desired result and then once you get the desired result, change the color of those letters to the color of your choice.
AseelHadlaq 15-Apr-13 15:59pm
I figured out the mechanism I just wanna know how to apply it in using c#
joshrduncan2012 15-Apr-13 16:23pm
Are you familiar with the properties of how strings can be manipulated?
Prasad Khandekar 15-Apr-13 15:51pm

What you are really looking for is a Rich Text Editor. Just search Code Project on RichText Editor, You shall get very good examples.


AseelHadlaq 15-Apr-13 16:00pm
I dont think that Rich text editors is what I need ,, thnx
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can use
string.replace("AAA", "<span style="color: rgb(255,0,0);">AAA</span>")

in code behind when building your text.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


Try this Code - Using Regex

//Store orginal text in hdnSearchText.Value (HiddenField)
 public string HighlightText(string argText)
     string Search_Str = hdnSearchText.Value.ToString();
     // Setup the regular expression and add the Or operator.
     Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
     // Highlight keywords by calling the
     //delegate each time a keyword is found.
     return RegExp.Replace(argText, new MatchEvaluator(ReplaceKeyWords));
 public string ReplaceKeyWords(Match m)
   return ("" + m.Value + "");
 //Search Button Click Code
 protected void btnSearch_Click(object sender,EventArgs e)
  string arg = txtSearch.Text; // Get Search Term
  string result = HighlightText(arg);
  lblDisplay.Text = result; //Display result in any label or html editor 


Siva Rm K
AseelHadlaq 16-Apr-13 4:48am
I'll try it thanx :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Rate this: bad
Please Sign up or sign in to vote.

Solution 5

try this !!
StringBuilder builder = new StringBuilder();
          builder.Append("<span style=\"color:Blue;\">");
          builder.Append("[text to highlight]");
          lbl.Text = builder.ToString();
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

I came up with this segment :

protected void Button1_Click(object sender, EventArgs e)

while (i < text.Length)
sub = text.Substring(i, Wl);
if (sub == st)
res += "" + st + "";
i = i + Wl;


res += text.Substring(i, 1);

MyLabel.Text = res;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100