Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
4.00/5 (1 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:
ABABBBBCGHTGHGAAAHHHGHGHGHGHGHA

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
Comments
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 asp.net 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    
Hello,

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

1. http://www.codeproject.com/Tips/455129/Rich-Text-Editor-with-ASP-NET.aspx
2. http://www.codeproject.com/KB/string/RTFBuilder.aspx
3. http://www.codeproject.com/KB/miscctrl/richtextboxextended.aspx


Regards,
AseelHadlaq 15-Apr-13 16:00pm    
I dont think that Rich text editors is what I need ,, thnx

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

in code behind when building your text.
 
Share this answer
 
v2
Hi,

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 
 }


Thanks

Siva Rm K
 
Share this answer
 
Comments
AseelHadlaq 16-Apr-13 4:48am    
I'll try it thanx :)
 
Share this answer
 
try this !!
XML
StringBuilder builder = new StringBuilder();
          builder.Append("adf");
          builder.Append("<span style=\"color:Blue;\">");
          builder.Append("[text to highlight]");
          builder.Append("</span>");
          lbl.Text = builder.ToString();
 
Share this answer
 
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;

}

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


}
MyLabel.Text = res;
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900