📄 frmfind.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace PublicUIComponent
{
/// <summary>
/// frmFind 的摘要说明。
/// </summary>
public class frmFind : System.Windows.Forms.Form
{
#region Windows 窗体设计器生成的代码
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
public System.Windows.Forms.ComboBox cboFind;
public System.Windows.Forms.ComboBox cboReplace;
private System.Windows.Forms.CheckBox chkWholeWord;
private System.Windows.Forms.CheckBox chkMatchCase;
private System.Windows.Forms.Button btnGoHome;
private System.Windows.Forms.Button btnFindNext;
private System.Windows.Forms.Button btnReplace;
private System.Windows.Forms.Button btnReplaceAll;
private System.Windows.Forms.Button btnClose;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public frmFind()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmFind));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.cboFind = new System.Windows.Forms.ComboBox();
this.cboReplace = new System.Windows.Forms.ComboBox();
this.chkWholeWord = new System.Windows.Forms.CheckBox();
this.chkMatchCase = new System.Windows.Forms.CheckBox();
this.btnGoHome = new System.Windows.Forms.Button();
this.btnFindNext = new System.Windows.Forms.Button();
this.btnReplace = new System.Windows.Forms.Button();
this.btnReplaceAll = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(38, 22);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(91, 17);
this.label1.TabIndex = 0;
this.label1.Text = "要查找的文本:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(38, 78);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(91, 17);
this.label2.TabIndex = 1;
this.label2.Text = "要替换的文本:";
//
// cboFind
//
this.cboFind.Location = new System.Drawing.Point(38, 46);
this.cboFind.Name = "cboFind";
this.cboFind.Size = new System.Drawing.Size(146, 20);
this.cboFind.TabIndex = 2;
this.cboFind.SelectedIndexChanged += new System.EventHandler(this.cboFind_SelectedIndexChanged);
this.cboFind.Leave += new System.EventHandler(this.cboFind_Leave);
//
// cboReplace
//
this.cboReplace.Location = new System.Drawing.Point(38, 94);
this.cboReplace.Name = "cboReplace";
this.cboReplace.Size = new System.Drawing.Size(146, 20);
this.cboReplace.TabIndex = 3;
this.cboReplace.Leave += new System.EventHandler(this.cboReplace_Leave);
//
// chkWholeWord
//
this.chkWholeWord.Location = new System.Drawing.Point(39, 150);
this.chkWholeWord.Name = "chkWholeWord";
this.chkWholeWord.TabIndex = 4;
this.chkWholeWord.Text = "全字匹配";
//
// chkMatchCase
//
this.chkMatchCase.Location = new System.Drawing.Point(39, 126);
this.chkMatchCase.Name = "chkMatchCase";
this.chkMatchCase.TabIndex = 5;
this.chkMatchCase.Text = "区分大小写";
//
// btnGoHome
//
this.btnGoHome.Location = new System.Drawing.Point(208, 16);
this.btnGoHome.Name = "btnGoHome";
this.btnGoHome.Size = new System.Drawing.Size(80, 24);
this.btnGoHome.TabIndex = 6;
this.btnGoHome.Text = "回到开头";
this.btnGoHome.Click += new System.EventHandler(this.btnGoHome_Click);
//
// btnFindNext
//
this.btnFindNext.Location = new System.Drawing.Point(208, 48);
this.btnFindNext.Name = "btnFindNext";
this.btnFindNext.Size = new System.Drawing.Size(80, 24);
this.btnFindNext.TabIndex = 7;
this.btnFindNext.Text = "查找下一个";
this.btnFindNext.Click += new System.EventHandler(this.btnFindNext_Click);
//
// btnReplace
//
this.btnReplace.Location = new System.Drawing.Point(208, 80);
this.btnReplace.Name = "btnReplace";
this.btnReplace.Size = new System.Drawing.Size(80, 24);
this.btnReplace.TabIndex = 8;
this.btnReplace.Text = "替换";
this.btnReplace.Click += new System.EventHandler(this.btnReplace_Click);
//
// btnReplaceAll
//
this.btnReplaceAll.Location = new System.Drawing.Point(208, 112);
this.btnReplaceAll.Name = "btnReplaceAll";
this.btnReplaceAll.Size = new System.Drawing.Size(80, 24);
this.btnReplaceAll.TabIndex = 9;
this.btnReplaceAll.Text = "全部替换";
this.btnReplaceAll.Click += new System.EventHandler(this.btnReplaceAll_Click);
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(208, 144);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(80, 24);
this.btnClose.TabIndex = 10;
this.btnClose.Text = "关闭";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// frmFind
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(328, 189);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnReplaceAll);
this.Controls.Add(this.btnReplace);
this.Controls.Add(this.btnFindNext);
this.Controls.Add(this.btnGoHome);
this.Controls.Add(this.chkMatchCase);
this.Controls.Add(this.chkWholeWord);
this.Controls.Add(this.cboReplace);
this.Controls.Add(this.cboFind);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "frmFind";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "查找与替换";
this.TopMost = true;
this.Closing += new System.ComponentModel.CancelEventHandler(this.frmFind_Closing);
this.ResumeLayout(false);
}
#endregion
//要处理的控件,从外界传入
public RichTextBox rt = null;
//当前查找的位置
private int curPos = 0;
#region 功能代码
/// <summary>
/// 查找下一个
/// </summary>
private void FindNext()
{
/*获取查找关键字*/
string WordToFind;
if(rt == null || this.cboFind.Text == String.Empty)
return;
WordToFind = this.cboFind.Text;
/*设置搜索选项*/
RichTextBoxFinds options = RichTextBoxFinds.None;
if(this.chkMatchCase.Checked)
{
options = options | RichTextBoxFinds.MatchCase;
}
if(this.chkWholeWord.Checked)
{
options = options | RichTextBoxFinds.WholeWord;
}
/*开始搜索*/
int iLoc;//查找的单词位置索引
iLoc = rt.Find(WordToFind,curPos,-1,options);
if(iLoc != -1)//如果找到
{
curPos = iLoc + WordToFind.Length;
}
else
{
curPos = 0;
MessageBox.Show("没有找到匹配项。","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
this.AddTextToList(this.cboFind);
}
/// <summary>
/// 将搜索过的单词加入列表
/// </summary>
/// <param name="cbb">ComboBox对象</param>
private void AddTextToList(ComboBox cbb)
{
if(cbb.Text == String.Empty)
{
return;
}
if(cbb.Items.IndexOf(cbb.Text) == -1)
{
cbb.Items.Add(cbb.Text);
}
}
/// <summary>
/// 替换选中的文字
/// </summary>
private void Replace()
{
int start,length;
start = this.rt.SelectionStart;
length = this.rt.SelectionLength;
if(this.cboReplace.Text == String.Empty || length == 0)//没有要替换的文字
return;
Font _font = this.rt.SelectionFont;//记录要替换的文字字体
Color _color = this.rt.SelectionColor;//记录要替换文字的颜色
this.rt.SelectedText = this.cboReplace.Text;//替换
//重新将文字设为选定状态
this.rt.SelectionStart = start;
this.rt.SelectionLength = this.cboReplace.Text.Length;
//重新设置文字的字体和颜色
if(_font != null)
{
this.rt.SelectionFont = _font;
}
this.rt.SelectionColor = _color;
this.AddTextToList(cboReplace);
}
/// <summary>
/// 全部替换
/// </summary>
private void ReplaceAll()
{
/*获取查找关键字*/
string WordToFind;//要查找的单词
if(this.rt == null || this.cboFind.Text == String.Empty)
return;
WordToFind = this.cboFind.Text;
/*设置搜索选项*/
RichTextBoxFinds options = RichTextBoxFinds.None;
if(this.chkMatchCase.Checked)
{
options = options | RichTextBoxFinds.MatchCase;
}
if(this.chkWholeWord.Checked)
{
options = options | RichTextBoxFinds.WholeWord;
}
int i,j,count,start;
/*在全部文本中查找WordToFind*/
i = this.rt.Find(WordToFind,0,-1,options);
if(i != -1)//找到WordToFind第一次出现的位置
{
j = 0;//保存每次调用Find的结果
count = 0;//初始化替换次数计数器
while(j != -1)//不断替换
{
start = this.rt.SelectionStart;
//记录字体信息
Font _font = this.rt.SelectionFont;
Color _color = this.rt.SelectionColor;
//实现替换
this.rt.SelectedText = this.cboReplace.Text;
//设置替换后字体与替换前一致
if(_font != null)
{
this.rt.SelectionFont = _font;
}
this.rt.SelectionColor = _color;
count++;//统计替换次数
//从新的位置进行查找
j = this.rt.Find(WordToFind, start + WordToFind.Length, -1, options);
}
MessageBox.Show("替换完成,共替换 " + count + " 处。","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
MessageBox.Show("没有找到匹配项。","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}
#endregion
private void btnGoHome_Click(object sender, System.EventArgs e)
{
this.curPos = 0;
this.rt.SelectionStart = 0;
this.rt.SelectionLength = 0;
}
private void btnFindNext_Click(object sender, System.EventArgs e)
{
this.FindNext();
}
private void btnReplace_Click(object sender, System.EventArgs e)
{
this.Replace();
}
private void btnReplaceAll_Click(object sender, System.EventArgs e)
{
this.ReplaceAll();
}
private void cboFind_SelectedIndexChanged(object sender, System.EventArgs e)
{//内容改变时从头开始搜索
this.curPos = 0;
}
private void btnClose_Click(object sender, System.EventArgs e)
{
this.Hide();
}
private void cboFind_Leave(object sender, System.EventArgs e)
{
this.AddTextToList(cboFind);
}
private void cboReplace_Leave(object sender, System.EventArgs e)
{
this.AddTextToList(cboReplace);
}
private void frmFind_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.Hide();
e.Cancel = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -