ohbutt69

rtf find form

Dec 7th, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace Rich_Text_Editor
  12. {
  13.     public partial class frmFindReplace : Form
  14.     {
  15.         public frmFindReplace()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.         RichTextBox frm1RTB;// declares the frm1RTB variable
  20.         int textFound;// declares the textFound variable
  21.         public void btnFind_Click(object sender, EventArgs e)//finds text specified in the txtSearch text box
  22.         {
  23.             frm1RTB = ((frmRichTextEditor)this.Owner).rtbContent;//; assigns the frm1 rtb variable the value of the rich text box in form 1
  24.             textFound = frm1RTB.Text.IndexOf(txtSearch.Text);// sets the textFound variable to the index of text value of the txtSearch box found in the frm1RTB
  25.             if (textFound != -1)// if textFound is not -1 (if there is text found)
  26.             {
  27.                 int selectionLength = txtSearch.TextLength;// sets the selectionLength variable to the length of the control text that was searched
  28.                 frm1RTB.Select(textFound, selectionLength);// selects the text in the richtextbox at the index of the text found for the selection length
  29.                 frm1RTB.Focus();// focus on the form 1 rich text box
  30.                 btnFindNext.Enabled = true;// enable the find next button
  31.                 btnReplace.Enabled = true;// enable the replace button
  32.                 btnReplaceAll.Enabled = true;// enable the replace all button
  33.             }
  34.             else// else (if there is no text found)
  35.             {
  36.                 MessageBox.Show("Text Was Not Found", "Error");// tells the user there was no text found in the rich text box
  37.             }
  38.         }
  39.         public void btnFindNext_Click(object sender, EventArgs e)// this button finds the next value in the rtb, after the first value was found.
  40.         {
  41.             frm1RTB = ((frmRichTextEditor)this.Owner).rtbContent;// assigns the frm1 rtb variable the value of the rich text box in form 1
  42.             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
  43.             if (textFound != -1)// if textFound is not -1 (if there is text found)
  44.             {
  45.                 int selectionLength = txtSearch.TextLength;// sets the selectionLength variable to the length of the control text that was searched
  46.                 frm1RTB.Select(textFound, selectionLength);// selects the text in the richtextbox at the index of the text found for the selection length
  47.                 frm1RTB.Focus();// focus on the form 1 rich text box
  48.             }
  49.             else// else (if there is no text found)
  50.             {
  51.                 btnReplace.Enabled = false;// disable the replace button
  52.                 btnReplaceAll.Enabled = false;// disable the replace all button
  53.                 MessageBox.Show("Text Was Not Found", "Error");// tells the user there was no text found in the rich text box
  54.             }
  55.         }
  56.         public void btnReplace_Click(object sender, EventArgs e)// replaces the current selected item supplied by the find function
  57.         {
  58.             {
  59.                 frm1RTB = ((frmRichTextEditor)this.Owner).rtbContent;// assigns the frm1 rtb variable the value of the rich text box in form 1
  60.                 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
  61.                 if (textFound != -1 && txtReplace.Text != "")// if there was text found and the txtReplace text box is not nothing
  62.                 {
  63.                     int selectionLength = txtSearch.TextLength;// sets the selectionLength variable to the length of the control text that was searched
  64.                     frm1RTB.Select(textFound, selectionLength);// selects the text in the richtextbox at the index of the text found for the selection length
  65.                     frm1RTB.SelectedText = frm1RTB.SelectedText.Replace(frm1RTB.SelectedText, txtReplace.Text);// replaces the selected text with the text in the txtReplace textbox
  66.                     frm1RTB.Focus();// focus on the form 1 rich text box
  67.                 }
  68.             }
  69.         }
  70.         public void btnReplaceAll_Click(object sender, EventArgs e)//replaces all found instances of the found text with another text value
  71.         {
  72.             if (txtReplace.Text != "")// if the txtreplace text box has text in it
  73.             {
  74.                 frm1RTB.Rtf = frm1RTB.Rtf.Replace(txtSearch.Text, txtReplace.Text);// replace all instances of the found text with the value in the txtReplace text box
  75.             }
  76.         }
  77.         private void txtSearch_TextChanged(object sender, EventArgs e)// this event handler fires when the text in the txtSearch text box is changed
  78.         {
  79.             if (txtSearch.Text.Length > 0)// if the length of the text is greater than 0
  80.             {
  81.                 btnFind.Enabled = true;// enable the find button
  82.             }
  83.             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
  84.             {
  85.                 btnFind.Enabled = false;// disable the find button
  86.                 btnFindNext.Enabled = false;// disable the findNext button
  87.             }
  88.         }
  89.     }
  90. }// end of program
  91.  
Advertisement
Add Comment
Please, Sign In to add comment