Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- namespace Rich_Text_Editor
- {
- public partial class frmFindReplace : Form
- {
- public frmFindReplace()
- {
- InitializeComponent();
- }
- RichTextBox frm1RTB;// declares the frm1RTB variable
- int textFound;// declares the textFound variable
- public void btnFind_Click(object sender, EventArgs e)//finds text specified in the txtSearch text box
- {
- frm1RTB = ((frmRichTextEditor)this.Owner).rtbContent;//; assigns the frm1 rtb variable the value of the rich text box in form 1
- textFound = frm1RTB.Text.IndexOf(txtSearch.Text);// sets the textFound variable to the index of text value of the txtSearch box found in the frm1RTB
- if (textFound != -1)// if textFound is not -1 (if there is text found)
- {
- int selectionLength = txtSearch.TextLength;// sets the selectionLength variable to the length of the control text that was searched
- frm1RTB.Select(textFound, selectionLength);// selects the text in the richtextbox at the index of the text found for the selection length
- frm1RTB.Focus();// focus on the form 1 rich text box
- btnFindNext.Enabled = true;// enable the find next button
- btnReplace.Enabled = true;// enable the replace button
- btnReplaceAll.Enabled = true;// enable the replace all button
- }
- else// else (if there is no text found)
- {
- MessageBox.Show("Text Was Not Found", "Error");// tells the user there was no text found in the rich text box
- }
- }
- public void btnFindNext_Click(object sender, EventArgs e)// this button finds the next value in the rtb, after the first value was found.
- {
- frm1RTB = ((frmRichTextEditor)this.Owner).rtbContent;// assigns the frm1 rtb variable the value of the rich text box in form 1
- textFound = frm1RTB.Text.IndexOf(txtSearch.Text, textFound+1);// sets the text found variable to the index of the last found text +1, so it finds the bext whole match
- if (textFound != -1)// if textFound is not -1 (if there is text found)
- {
- int selectionLength = txtSearch.TextLength;// sets the selectionLength variable to the length of the control text that was searched
- frm1RTB.Select(textFound, selectionLength);// selects the text in the richtextbox at the index of the text found for the selection length
- frm1RTB.Focus();// focus on the form 1 rich text box
- }
- else// else (if there is no text found)
- {
- btnReplace.Enabled = false;// disable the replace button
- btnReplaceAll.Enabled = false;// disable the replace all button
- MessageBox.Show("Text Was Not Found", "Error");// tells the user there was no text found in the rich text box
- }
- }
- public void btnReplace_Click(object sender, EventArgs e)// replaces the current selected item supplied by the find function
- {
- {
- frm1RTB = ((frmRichTextEditor)this.Owner).rtbContent;// assigns the frm1 rtb variable the value of the rich text box in form 1
- textFound = frm1RTB.Text.IndexOf(txtSearch.Text);// sets the text found variable to the index of the last found text +1, so it finds the bext whole match
- if (textFound != -1 && txtReplace.Text != "")// if there was text found and the txtReplace text box is not nothing
- {
- int selectionLength = txtSearch.TextLength;// sets the selectionLength variable to the length of the control text that was searched
- frm1RTB.Select(textFound, selectionLength);// selects the text in the richtextbox at the index of the text found for the selection length
- frm1RTB.SelectedText = frm1RTB.SelectedText.Replace(frm1RTB.SelectedText, txtReplace.Text);// replaces the selected text with the text in the txtReplace textbox
- frm1RTB.Focus();// focus on the form 1 rich text box
- }
- }
- }
- public void btnReplaceAll_Click(object sender, EventArgs e)//replaces all found instances of the found text with another text value
- {
- if (txtReplace.Text != "")// if the txtreplace text box has text in it
- {
- frm1RTB.Rtf = frm1RTB.Rtf.Replace(txtSearch.Text, txtReplace.Text);// replace all instances of the found text with the value in the txtReplace text box
- }
- }
- private void txtSearch_TextChanged(object sender, EventArgs e)// this event handler fires when the text in the txtSearch text box is changed
- {
- if (txtSearch.Text.Length > 0)// if the length of the text is greater than 0
- {
- btnFind.Enabled = true;// enable the find button
- }
- else if (txtSearch.Text.Length <= 0)// else if the length of the text in the txtSearch text box is less than or equal to 0
- {
- btnFind.Enabled = false;// disable the find button
- btnFindNext.Enabled = false;// disable the findNext button
- }
- }
- }
- }// end of program
Advertisement
Add Comment
Please, Sign In to add comment