Friday, June 12, 2009

How to get the word count and highlighting the word in a stream

private void word_count_Load(object sender, EventArgs e)
{
StreamReader sr = new StreamReader(Form1.currpath + "sampl.txt");
//richTextBox1.Text = sr.ReadLine();
string contents = sr.ReadToEnd();
StreamWriter sw = new StreamWriter("richTextBox1");
sw.Write(contents);
sw.Close();
richTextBox1.Text = contents;
sr.Close();
label2.Text=Form1.query.ToString();
//string[] str = richTextBox1.Text.Split('');
string text = contents.ToString();

string word = label2.Text.ToString();
r = new Regex(@"\b" + word + @"\b", RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(contents);
a = mc.Count;
//int a = str.Length;
textBox1.Text = a.ToString();
//int b = label2.Text.Length;
//int d = richTextBox1.Find(label2.Text.ToString());
////int a = richTextBox1.Find(""); <br /> ////int b = richTextBox1.Find("");
////int c = d + b;
//richTextBox1.Select(d, b);
//richTextBox1.SelectionColor = Color.Goldenrod;
//richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, FontStyle.Bold);



string zoek = label2.Text;

int index = richTextBox1.Text.IndexOf(zoek);
while(index != -1)
{
richTextBox1.Select(index, zoek.Length);
richTextBox1.SelectionColor = Color.Red;
index = richTextBox1.Text.IndexOf(zoek, index + zoek.Length);
}



}

No comments:

Post a Comment