⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 searchandreplace.cs

📁 c文本编辑器源码.............
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Editor
{
	/// <summary>
	/// SearchAndReplace 的摘要说明。
	/// </summary>
	public class SearchAndReplace : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txtSearch;
		private System.Windows.Forms.RadioButton radioUp;
		private System.Windows.Forms.RadioButton radioDown;
		private System.Windows.Forms.CheckBox checkCase;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button btnSearch;
		private System.Windows.Forms.Button btnReplace;
		private System.Windows.Forms.Button btnReplaceAll;
		private System.Windows.Forms.TextBox txtReplace;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private MainForm parentForm;

		public SearchAndReplace(MainForm parent)
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			this.parentForm = parent;

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.txtSearch = new System.Windows.Forms.TextBox();
			this.radioUp = new System.Windows.Forms.RadioButton();
			this.radioDown = new System.Windows.Forms.RadioButton();
			this.checkCase = new System.Windows.Forms.CheckBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txtReplace = new System.Windows.Forms.TextBox();
			this.btnSearch = new System.Windows.Forms.Button();
			this.btnReplace = new System.Windows.Forms.Button();
			this.btnReplaceAll = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "查找内容";
			// 
			// txtSearch
			// 
			this.txtSearch.Location = new System.Drawing.Point(80, 8);
			this.txtSearch.Name = "txtSearch";
			this.txtSearch.Size = new System.Drawing.Size(160, 21);
			this.txtSearch.TabIndex = 1;
			this.txtSearch.Text = "";
			// 
			// radioUp
			// 
			this.radioUp.Location = new System.Drawing.Point(120, 72);
			this.radioUp.Name = "radioUp";
			this.radioUp.Size = new System.Drawing.Size(48, 24);
			this.radioUp.TabIndex = 2;
			this.radioUp.Text = "向上";
			// 
			// radioDown
			// 
			this.radioDown.Checked = true;
			this.radioDown.Location = new System.Drawing.Point(192, 72);
			this.radioDown.Name = "radioDown";
			this.radioDown.Size = new System.Drawing.Size(48, 24);
			this.radioDown.TabIndex = 3;
			this.radioDown.TabStop = true;
			this.radioDown.Text = "向下";
			// 
			// checkCase
			// 
			this.checkCase.Location = new System.Drawing.Point(16, 72);
			this.checkCase.Name = "checkCase";
			this.checkCase.Size = new System.Drawing.Size(88, 24);
			this.checkCase.TabIndex = 4;
			this.checkCase.Text = "区分大小写";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "替换为";
			// 
			// txtReplace
			// 
			this.txtReplace.Location = new System.Drawing.Point(80, 40);
			this.txtReplace.Name = "txtReplace";
			this.txtReplace.Size = new System.Drawing.Size(160, 21);
			this.txtReplace.TabIndex = 1;
			this.txtReplace.Text = "";
			// 
			// btnSearch
			// 
			this.btnSearch.Location = new System.Drawing.Point(272, 8);
			this.btnSearch.Name = "btnSearch";
			this.btnSearch.TabIndex = 5;
			this.btnSearch.Text = "查找";
			this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
			// 
			// btnReplace
			// 
			this.btnReplace.Location = new System.Drawing.Point(272, 40);
			this.btnReplace.Name = "btnReplace";
			this.btnReplace.TabIndex = 6;
			this.btnReplace.Text = "替换";
			this.btnReplace.Click += new System.EventHandler(this.btnReplace_Click);
			// 
			// btnReplaceAll
			// 
			this.btnReplaceAll.Location = new System.Drawing.Point(360, 40);
			this.btnReplaceAll.Name = "btnReplaceAll";
			this.btnReplaceAll.TabIndex = 7;
			this.btnReplaceAll.Text = "全部替换";
			this.btnReplaceAll.Click += new System.EventHandler(this.btnReplaceAll_Click);
			// 
			// SearchAndReplace
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(456, 101);
			this.Controls.Add(this.btnReplaceAll);
			this.Controls.Add(this.btnReplace);
			this.Controls.Add(this.btnSearch);
			this.Controls.Add(this.checkCase);
			this.Controls.Add(this.radioDown);
			this.Controls.Add(this.txtSearch);
			this.Controls.Add(this.txtReplace);
			this.Controls.Add(this.radioUp);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.label2);
			this.Name = "SearchAndReplace";
			this.Text = "查找替换";
			this.Load += new System.EventHandler(this.SearchAndReplace_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private string strSearch = ""; // 表示要查找的字符串
		private string strReplace = "";// 表示要替换成的字符串
		private int searchPos = 0,lastSearchPos = 0;// 前者表示当前的查找位置,后者表示上次的查找位置
		private void btnSearch_Click(object sender, System.EventArgs e)
		{
			this.strSearch = this.txtSearch.Text;
			SearchText(true);
		}

		private void btnReplace_Click(object sender, System.EventArgs e)
		{
			this.strSearch = this.txtSearch.Text;
			this.strReplace = this.txtReplace.Text;
			SearchText(true);
			if(this.parentForm.txtMain.SelectedText.Length > 0)
			{
				this.parentForm.txtMain.SelectedText = this.strReplace;
			}
		}

		private void btnReplaceAll_Click(object sender, System.EventArgs e)
		{
			this.strSearch = this.txtSearch.Text;
			this.strReplace = this.txtReplace.Text;
			while(SearchText(false))
			{
				if(this.parentForm.txtMain.SelectedText.Length > 0)
				{
					this.parentForm.txtMain.SelectedText = this.strReplace;
				}
			}
		}
		private bool SearchText(bool ShowNotFind)
			// 函数完成文本的查找功能,参数表示是否在未找到指定文本时显示消息,
			// 返回值为是否找到指定文本
		{
			bool find = true;
			if(this.checkCase.Checked)// 表示大小写匹配查找
			{
				if(this.radioDown.Checked) // 表示向下查找
				{
					this.searchPos = this.parentForm.txtMain.Find(this.strSearch,searchPos,
						this.parentForm.txtMain.Text.Length,RichTextBoxFinds.MatchCase);
				}
				else
				{
					this.searchPos = this.parentForm.txtMain.Find(this.strSearch,0,
						searchPos,RichTextBoxFinds.MatchCase | RichTextBoxFinds.Reverse);
				}
			}
			else
			{
				if(this.radioDown.Checked)
				{
					this.searchPos = this.parentForm.txtMain.Find(this.strSearch,searchPos,
						this.parentForm.txtMain.Text.Length,RichTextBoxFinds.None);
				}
				else
				{
					this.searchPos = this.parentForm.txtMain.Find(this.strSearch,0,
						searchPos,RichTextBoxFinds.Reverse);
				}
			}
			if(this.searchPos < 0)//如果未找到,则显示信息,将上次查找位置复原,置标志位为未找到
			{
				if(ShowNotFind)
				{
					MessageBox.Show("指定的文本未找到","文本编辑器",MessageBoxButtons.OK,
						MessageBoxIcon.Information);
				}
				this.searchPos = this.lastSearchPos;
				find = false;
			}
			else
			{
				if(this.radioDown.Checked)
				{
					this.searchPos += this.strSearch.Length;
				}
				else
				{
					this.searchPos -= this.strSearch.Length;
				}
				this.parentForm.Focus();// 使主窗体获得焦点
			}
			this.lastSearchPos = this.searchPos;
			return find;
		}

		private void SearchAndReplace_Load(object sender, System.EventArgs e)
		{
		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -