Startup class: program. CS
Using system;
Using system. Collections. Generic;
Using system. Windows. forms;
Namespace fontcolor
{
Static class Program
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. enablevisualstyles ();
Application. setcompatibletextrenderingdefault (false );
Application. Run (New frmcolorwords ());
}
}
}
Search Class: frmcolorwords. CS
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. collections;
Namespace fontcolor
{
Public partial class frmcolorwords: Form
{
Arraylist list = new arraylist ();
Public frmcolorwords ()
{
Initializecomponent ();
}
// Index words
Private void btnsearch_click (Object sender, eventargs E)
{
If (this. rtxttext. Text. Equals (""))
{
MessageBox. Show ("Please load the text to be indexed first! "," Message ");
This. btnloadtext. Focus ();
Return;
}
If (this.txt searchwords. Text. Equals (""))
{
MessageBox. Show ("Enter the word you want to index! "," Message ");
This.txt searchwords. Focus ();
Return;
}
Int place = 0;
This. rtxttext. selectall ();
This. rtxttext. selectioncolor = color. Black;
This. rtxttext. selectionfont = new font ("", 9 );
String text = "" + this. rtxttext. Text. tolower () + "";
String word = this.txt searchwords. Text. tolower (). Trim ();
Int S = 0; // Number of words displayed
If (text. indexof (word, place + 1) =-1)
{
MessageBox. Show ("not found! ");
}
Else
{
Try
{
For (INT I = 0; I <text. length; I ++)
{
Int start = text. indexof (word, place + 1 );
If (START =-1)
{
MessageBox. Show ("indexed word" + word + "finished! \ N total index to "+ S +! "," Message ");
Break;
}
String front = text. substring (START-1, 1 );
String back = text. substring (start + word. length, 1 );
If (list. Contains (Front) & list. Contains (back ))
{
S ++;
This. rtxttext. Select (START-1, word. Length );
This. rtxttext. selectioncolor = color. blue;
This. rtxttext. selectionfont = new font ("blod", 15 );
}
Place = start;
}
}
Catch {}
}
}
// Load text
Private void btnloadtext_click (Object sender, eventargs E)
{
Openfiledialog openfile = new openfiledialog ();
Openfile. Filter = "text file (*. txt) | *. txt | all files (*. *) | *.*";
If (openfile. showdialog () = dialogresult. OK)
{
This. rtxttext. Clear ();
String filename = openfile. filename;
This. rtxttext. LoadFile (filename, richtextboxstreamtype. plaintext );
This.txt searchwords. Focus ();
}
}
// Exit
Private void btnexit_click (Object sender, eventargs E)
{
This. Dispose ();
}
// Interface Loading
Private void frmcolorwords_load (Object sender, eventargs E)
{
String [] STR = {"","! "," @ "," # "," $ ",";",":","(",")","_", "+ ","-","*","/","? "," <","> ","/","\\","{","}","? "," = "," & "," % ","~ ",". "," \ "", "\ '", "[", "]"};
For (INT I = 0; I <Str. length; I ++)
{
List. Add (STR [I]);
}
}
}
}
----- Javascript:
Highlight query characters
// JavaScript document
Function wordhight (){
VaR OBJ = Document. getelementbyid ("objcontent ");
If (OBJ ){
VaR DIV = Document. getelementbyid ("objcontent ");
If (document. getelementbyid ("keyword "));
VaR keyword = Document. getelementbyid ("keyword"). value;
Markhighlight (DIV, keyword );
}
}
/*----------------------------------------*\
* Mark highlighted keywords by markcxz (markcxz@aol.com) with JS)
* Parameter description:
* OBJ: object. html tag nodes to be highlighted.
* Hlwords: String. Keywords to be highlighted. Multiple words are separated by vertical bars (|) or spaces.
* Cssclass: a string that defines the CSS pseudo class with the keyword highlighted display style.
* Reference: specific words on the page highlighted by JavaScript html dom by shawl. Qiu
\*----------------------------------------*/
Function markhighlight (OBJ, hlwords, cssclass ){
Hlwords = analyzehighlightwords (hlwords );
If (OBJ = NULL | hlwords. Length = 0)
Return;
If (cssclass = NULL)
Cssclass = "highlight ";
Markhighlightcore (OBJ, hlwords );
// ------------ Core method for executing highlight tags ----------------------------
Function markhighlightcore (OBJ, keywords ){
VaR Re = new Regexp (keywords, "I ");
For (VAR I = 0; I <obj. childnodes. length; I ++ ){
VaR childobj = obj. childnodes [I];
If (childobj. nodetype = 3 ){
If (childobj. Data. Search (re) =-1) continue;
VaR reresult = new Regexp ("(" + keywords + ")", "Gi ");
VaR objresult = Document. createelement ("span ");
Objresult. innerhtml = childobj. Data. Replace (reresult, "<SPAN class = '" + cssclass + "'> $1 </span> ");
If (childobj. Data = objresult. childnodes [0]. innerhtml) continue;
OBJ. replaceChild (objresult, childobj );
} Else if (childobj. nodetype = 1 ){
Markhighlightcore (childobj, keywords );
}
}
}
// ---------- Analysis keyword ----------------------
Function analyzehighlightwords (hlwords ){
If (hlwords = NULL) Return "";
Hlwords = hlwords. Replace (/\ s +/g, "|"). Replace (/\ | +/g, "| ");
Hlwords = hlwords. Replace (/(^ \ | *) | (\ | * $)/g ,"");
If (hlwords. Length = 0) Return "";
VaR wordsarr = hlwords. Split ("| ");
If (wordsarr. length> 1 ){
VaR resultarr = bubblesort (wordsarr );
VaR result = "";
For (VAR I = 0; I <resultarr. length; I ++ ){
Result = Result + "|" + resultarr [I];
}
Return result. Replace (/(^ \ | *) | (\ | * $)/g ,"");
} Else {
Return hlwords;
}
}
// ----- Use the bubble sort method to put long keywords in front -----
Function bubblesort (ARR ){
VaR temp, exchange;
For (VAR I = 0; I <arr. length; I ++ ){
Exchange = false;
For (var j = arr. Length-2; j> = I; j --){
If (ARR [J + 1]. Length)> (ARR [J]). Length ){
Temp = arr [J + 1]; arr [J + 1] = arr [J]; arr [J] = temp;
Exchange = true;
}
}
If (! Exchange) break;
}
Return arr;
}
}