frmReplace.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:6k
源码类别:

Email服务器

开发平台:

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 myWorkPad
  9. {
  10.     public partial class frmReplace : Form
  11.     {
  12.         // member variable pointing to main form
  13.         MainForm mMain;
  14.         
  15.         // default constructor
  16.         public frmReplace()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         // overloaded constructor accepteing main form as
  21.         // an argument
  22.         public frmReplace(MainForm f)
  23.         {
  24.             InitializeComponent();
  25.             mMain = f;
  26.         }
  27.         private void btnFind_Click(object sender, System.EventArgs e)
  28.         {
  29.             try
  30.             {
  31.                 int StartPosition;
  32.                 StringComparison SearchType;
  33.                 if (chkMatchCase.Checked == true)
  34.                 {
  35.                     SearchType = StringComparison.Ordinal;
  36.                 }
  37.                 else
  38.                 {
  39.                     SearchType = StringComparison.OrdinalIgnoreCase;
  40.                 }
  41.                 StartPosition = mMain.rtMain.Text.IndexOf(txtSearchTerm.Text, SearchType);
  42.                 if (StartPosition == 0)
  43.                 {
  44.                     MessageBox.Show("字符串: " + txtSearchTerm.Text.ToString() + "没有找到!", "没有匹配", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  45.                     return;
  46.                 }
  47.                 mMain.rtMain.Select(StartPosition, txtSearchTerm.Text.Length);
  48.                 mMain.rtMain.ScrollToCaret();
  49.                 mMain.Focus();
  50.                 btnFindNext.Enabled = true;
  51.             }
  52.             catch (Exception ex)
  53.             {
  54.                 MessageBox.Show(ex.Message.ToString(), "Error");
  55.             }
  56.         }
  57.         private void btnFindNext_Click(object sender, System.EventArgs e)
  58.         {
  59.             try
  60.             {
  61.                 int StartPosition = mMain.rtMain.SelectionStart + 2;
  62.                 StringComparison SearchType;
  63.                 if (chkMatchCase.Checked == true)
  64.                 {
  65.                     SearchType = StringComparison.Ordinal;
  66.                 }
  67.                 else
  68.                 {
  69.                     SearchType = StringComparison.OrdinalIgnoreCase;
  70.                 }
  71.                 StartPosition = mMain.rtMain.Text.IndexOf(txtSearchTerm.Text, StartPosition, SearchType);
  72.                 if (StartPosition == 0 || StartPosition < 0)
  73.                 {
  74.                     MessageBox.Show("String: " + txtSearchTerm.Text.ToString() + " not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  75.                     return;
  76.                 }
  77.                 mMain.rtMain.Select(StartPosition, txtSearchTerm.Text.Length);
  78.                 mMain.rtMain.ScrollToCaret();
  79.                 mMain.Focus();
  80.             }
  81.             catch (Exception ex)
  82.             {
  83.                 MessageBox.Show(ex.Message.ToString(), "Error");
  84.             }
  85.         }
  86.         private void btnReplace_Click(object sender, System.EventArgs e)
  87.         {
  88.             try
  89.             {
  90.                 if (mMain.rtMain.SelectedText.Length != 0)
  91.                 {
  92.                     mMain.rtMain.SelectedText = txtReplacementText.Text;
  93.                 }
  94.                 int StartPosition;
  95.                 StringComparison SearchType;
  96.                 if (chkMatchCase.Checked == true)
  97.                 {
  98.                     SearchType = StringComparison.Ordinal;
  99.                 }
  100.                 else
  101.                 {
  102.                     SearchType = StringComparison.OrdinalIgnoreCase;
  103.                 }
  104.                 StartPosition = mMain.rtMain.Text.IndexOf(txtSearchTerm.Text, SearchType);
  105.                 if (StartPosition == 0 || StartPosition < 0)
  106.                 {
  107.                     MessageBox.Show("字符串: " + txtSearchTerm.Text.ToString() + "没有找到!", "没有匹配", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
  108.                     return;
  109.                 }
  110.                 mMain.rtMain.Select(StartPosition, txtSearchTerm.Text.Length);
  111.                 mMain.rtMain.ScrollToCaret();
  112.                // mMain.Focus();
  113.             }
  114.             catch (Exception ex)
  115.             {
  116.                 MessageBox.Show(ex.Message.ToString(), "Error");
  117.             }
  118.         }
  119.         private void btnReplaceAll_Click(object sender, System.EventArgs e)
  120.         {
  121.             try
  122.             {
  123.                 mMain.rtMain.Rtf = mMain.rtMain.Rtf.Replace(txtSearchTerm.Text.Trim(), txtReplacementText.Text.Trim());
  124.                 int StartPosition;
  125.                 StringComparison SearchType;
  126.                 if (chkMatchCase.Checked == true)
  127.                 {
  128.                     SearchType = StringComparison.Ordinal;
  129.                 }
  130.                 else
  131.                 {
  132.                     SearchType = StringComparison.OrdinalIgnoreCase;
  133.                 }
  134.                 StartPosition = mMain.rtMain.Text.IndexOf(txtReplacementText.Text, SearchType);
  135.                 mMain.rtMain.Select(StartPosition, txtReplacementText.Text.Length);
  136.                 mMain.rtMain.ScrollToCaret();
  137.                 //mMain.Focus();
  138.             }
  139.             catch (Exception ex)
  140.             {
  141.                 MessageBox.Show(ex.Message.ToString(), "Error");
  142.             }
  143.         }
  144.         private void txtSearchTerm_TextChanged(object sender, EventArgs e)
  145.         {
  146.             btnFindNext.Enabled = false;
  147.         }
  148.         
  149.     }
  150. }