using System.IO;
private void button1_Click(object sender, EventArgs e) { if (ofd.ShowDialog() == DialogResult.OK) { //get the filename of the text file string filename = ofd.FileName; //read all the contents string textfile = File.ReadAllText(filename); // display the contents in the richtextbox rtDisplay.Text = textfile; } }
private void button2_Click(object sender, EventArgs e) { //string words = txtSearch.Text; //rtDisplay.Text = ""; //rtDisplay.Text = words; int index = 0; // if the textbox has an inputted text while (index < rtDisplay.Text.LastIndexOf(txtSearch.Text)){ //find text in the searched word rtDisplay.Find(txtSearch.Text,rtDisplay.TextLength, RichTextBoxFinds.None); // highlights the word with a color yellow rtDisplay.SelectionBackColor = Color.Yellow; // highlights all those words that is the same with the searched word in the textbox index = rtDisplay.Text.IndexOf(txtSearch.Text, index) + 1; } }