替换.cs
上传用户:hueiseng
上传日期:2022-07-26
资源大小:156k
文件大小:4k
源码类别:

C#编程

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace 笔记本
  9. {
  10.     public partial class 替换 : Form
  11.     {
  12.          int findplace = 0;
  13.         public 替换()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private void button1_Click(object sender, EventArgs e)
  18.         {
  19.             Form1 f1 = (Form1)this.Owner;
  20.             f1.Activate();
  21.             if (textBox1.Text.Length > 0)
  22.             {
  23.                 if (!checkBox1.Checked)
  24.                 {
  25.                         findplace = f1.richTextBox1.Text.IndexOf(textBox1.Text, findplace);
  26.                         if (findplace == -1)
  27.                         {
  28.                             MessageBox.Show("无法找到搜索项!");
  29.                             findplace = f1.richTextBox1.SelectionStart;
  30.                         }
  31.                         else
  32.                         {
  33.                             f1.richTextBox1.Select(findplace, textBox1.Text.Length);
  34.                             findplace = f1.richTextBox1.SelectionStart + textBox1.Text.Length;
  35.                         }
  36.                     }
  37.              
  38.                 else
  39.                 {
  40.                         findplace = f1.richTextBox1.Text.IndexOf(textBox1.Text, findplace, StringComparison.OrdinalIgnoreCase);
  41.                         if (findplace == -1)
  42.                         {
  43.                             MessageBox.Show("无法找到搜索项!");
  44.                             findplace = f1.richTextBox1.SelectionStart;
  45.                         }
  46.                         else
  47.                         {
  48.                             f1.richTextBox1.Select(findplace, textBox1.Text.Length);
  49.                             findplace = f1.richTextBox1.SelectionStart + textBox1.Text.Length;
  50.                         }
  51.                     }
  52.                 }
  53.             }
  54.         private void button2_Click(object sender, EventArgs e)
  55.         {
  56.             Form1 f1 = (Form1)this.Owner;
  57.             f1.Activate();
  58.             if (textBox1.Text.Length != 0&&textBox2.Text.Length != 0)
  59.             {
  60.                 f1.richTextBox1.SelectedText = f1.richTextBox1.SelectedText.Replace( f1.richTextBox1.SelectedText, textBox2.Text);
  61.             }
  62.             else
  63.                 MessageBox.Show("please fill the text.");
  64.                     
  65.            
  66.         }
  67.         private void button3_Click(object sender, EventArgs e)
  68.         {
  69.             Form1 f1 = (Form1)this.Owner;
  70.             f1.Activate();
  71.             if (!checkBox1.Checked)
  72.             {
  73.                 if (textBox1.Text.Length != 0 && textBox2.Text.Length != 0)
  74.                 {
  75.                     f1.richTextBox1.Text = f1.richTextBox1.Text.Replace(textBox1.Text, textBox2.Text);
  76.                 }
  77.                 else
  78.                     MessageBox.Show("please fill the text.");
  79.             }
  80.             else
  81.             {
  82.                 findplace = 0;
  83.                
  84.                     while (findplace >= 0)
  85.                     {
  86.                         findplace = f1.richTextBox1.Text.IndexOf(textBox1.Text, findplace, StringComparison.OrdinalIgnoreCase);
  87.                         if (findplace == -1)
  88.                             return;
  89.                         f1.richTextBox1.Select(findplace, textBox1.Text.Length);
  90.                        
  91.                          if (textBox1.Text.Length != 0 && textBox2.Text.Length != 0)
  92.                              f1.richTextBox1.SelectedText = f1.richTextBox1.SelectedText.Replace(f1.richTextBox1.SelectedText, textBox2.Text);
  93.                            else
  94.                         {
  95.                             MessageBox.Show("please fill the text.");
  96.                             return;
  97.                         }
  98.                     }
  99.              ;
  100.             }
  101.         }
  102.     }
  103. }