Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Text
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 15-Apr-13 10:26am
Comments
joshrduncan2012 at 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 at 15-Apr-13 15:59pm
   
I figured out the mechanism I just wanna know how to apply it in asp.net using c#
joshrduncan2012 at 15-Apr-13 16:23pm
   
Are you familiar with the properties of how strings can be manipulated?
Prasad Khandekar at 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 at 15-Apr-13 16:00pm
   
I dont think that Rich text editors is what I need ,, thnx
Rate this: bad
good
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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
AseelHadlaq at 16-Apr-13 4:48am
   
I'll try it thanx :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

<xml
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

try this !!
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();
  Permalink  
Rate this: bad
good
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;
 
}
 
else
{
res += text.Substring(i, 1);
i++;
}
 

}
MyLabel.Text = res;
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 Andreas Gieriet 90
4 Jochen Arndt 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 16 Apr 2013
Copyright © CodeProject, 1999-2014
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