frmReplace.cs
上传用户:jsz11269
上传日期:2017-01-14
资源大小:450k
文件大小:6k
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- namespace myWorkPad
- {
- public partial class frmReplace : Form
- {
- // member variable pointing to main form
- MainForm mMain;
-
- // default constructor
- public frmReplace()
- {
- InitializeComponent();
- }
- // overloaded constructor accepteing main form as
- // an argument
- public frmReplace(MainForm f)
- {
- InitializeComponent();
- mMain = f;
- }
- private void btnFind_Click(object sender, System.EventArgs e)
- {
- try
- {
- int StartPosition;
- StringComparison SearchType;
- if (chkMatchCase.Checked == true)
- {
- SearchType = StringComparison.Ordinal;
- }
- else
- {
- SearchType = StringComparison.OrdinalIgnoreCase;
- }
- StartPosition = mMain.rtMain.Text.IndexOf(txtSearchTerm.Text, SearchType);
- if (StartPosition == 0)
- {
- MessageBox.Show("字符串: " + txtSearchTerm.Text.ToString() + "没有找到!", "没有匹配", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- return;
- }
- mMain.rtMain.Select(StartPosition, txtSearchTerm.Text.Length);
- mMain.rtMain.ScrollToCaret();
- mMain.Focus();
- btnFindNext.Enabled = true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString(), "Error");
- }
- }
- private void btnFindNext_Click(object sender, System.EventArgs e)
- {
- try
- {
- int StartPosition = mMain.rtMain.SelectionStart + 2;
- StringComparison SearchType;
- if (chkMatchCase.Checked == true)
- {
- SearchType = StringComparison.Ordinal;
- }
- else
- {
- SearchType = StringComparison.OrdinalIgnoreCase;
- }
- StartPosition = mMain.rtMain.Text.IndexOf(txtSearchTerm.Text, StartPosition, SearchType);
- if (StartPosition == 0 || StartPosition < 0)
- {
- MessageBox.Show("String: " + txtSearchTerm.Text.ToString() + " not found", "No Matches", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- return;
- }
- mMain.rtMain.Select(StartPosition, txtSearchTerm.Text.Length);
- mMain.rtMain.ScrollToCaret();
- mMain.Focus();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString(), "Error");
- }
- }
- private void btnReplace_Click(object sender, System.EventArgs e)
- {
- try
- {
- if (mMain.rtMain.SelectedText.Length != 0)
- {
- mMain.rtMain.SelectedText = txtReplacementText.Text;
- }
- int StartPosition;
- StringComparison SearchType;
- if (chkMatchCase.Checked == true)
- {
- SearchType = StringComparison.Ordinal;
- }
- else
- {
- SearchType = StringComparison.OrdinalIgnoreCase;
- }
- StartPosition = mMain.rtMain.Text.IndexOf(txtSearchTerm.Text, SearchType);
- if (StartPosition == 0 || StartPosition < 0)
- {
- MessageBox.Show("字符串: " + txtSearchTerm.Text.ToString() + "没有找到!", "没有匹配", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
- return;
- }
- mMain.rtMain.Select(StartPosition, txtSearchTerm.Text.Length);
- mMain.rtMain.ScrollToCaret();
- // mMain.Focus();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString(), "Error");
- }
- }
- private void btnReplaceAll_Click(object sender, System.EventArgs e)
- {
- try
- {
- mMain.rtMain.Rtf = mMain.rtMain.Rtf.Replace(txtSearchTerm.Text.Trim(), txtReplacementText.Text.Trim());
- int StartPosition;
- StringComparison SearchType;
- if (chkMatchCase.Checked == true)
- {
- SearchType = StringComparison.Ordinal;
- }
- else
- {
- SearchType = StringComparison.OrdinalIgnoreCase;
- }
- StartPosition = mMain.rtMain.Text.IndexOf(txtReplacementText.Text, SearchType);
- mMain.rtMain.Select(StartPosition, txtReplacementText.Text.Length);
- mMain.rtMain.ScrollToCaret();
- //mMain.Focus();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message.ToString(), "Error");
- }
- }
- private void txtSearchTerm_TextChanged(object sender, EventArgs e)
- {
- btnFindNext.Enabled = false;
- }
-
- }
- }