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

📄 searchform.cs

📁 Csharp实例编程百例.rar
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace SearchTextApp
{
	/// <summary>
	/// SearchForm 的摘要说明。
	/// </summary>
	public class SearchForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnFindNext;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.TextBox txtToSearch;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

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

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

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

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.txtToSearch = new System.Windows.Forms.TextBox();
			this.btnFindNext = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 40);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(80, 32);
			this.label1.TabIndex = 0;
			this.label1.Text = "查找内容:";
			// 
			// txtToSearch
			// 
			this.txtToSearch.Location = new System.Drawing.Point(120, 40);
			this.txtToSearch.Name = "txtToSearch";
			this.txtToSearch.Size = new System.Drawing.Size(272, 21);
			this.txtToSearch.TabIndex = 1;
			this.txtToSearch.Text = "";
			// 
			// btnFindNext
			// 
			this.btnFindNext.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnFindNext.Location = new System.Drawing.Point(168, 103);
			this.btnFindNext.Name = "btnFindNext";
			this.btnFindNext.Size = new System.Drawing.Size(104, 25);
			this.btnFindNext.TabIndex = 2;
			this.btnFindNext.Text = "查找下一处(&F)";
			this.btnFindNext.Click += new System.EventHandler(this.btnFindNext_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnCancel.Location = new System.Drawing.Point(288, 103);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(96, 25);
			this.btnCancel.TabIndex = 3;
			this.btnCancel.Text = "取消";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// SearchForm
			// 
			this.AcceptButton = this.btnFindNext;
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(408, 141);
			this.ControlBox = false;
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnCancel,
																		  this.btnFindNext,
																		  this.txtToSearch,
																		  this.label1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "SearchForm";
			this.ShowInTaskbar = false;
			this.Text = "查找";
			this.TopMost = true;
			this.ResumeLayout(false);

		}
		#endregion

		private void btnCancel_Click(object sender, System.EventArgs e)
		{	//隐藏SearchForm窗口
			this.Hide ();
		}

		private int FindPlace = 0;		//文本查找位置,缺省为文本起点
		private void btnFindNext_Click(object sender, System.EventArgs e)
		{
			if (txtToSearch.Text != "")	//当查找内容不为空时,进行查找
			{
				//获得Form1窗体的引用
				Form1 mainForm = (Form1)this.Owner ;
				//如果主窗体文本内容不为空,进行查找
				if (mainForm.MyRTBox .Text .Length >0)
				{
					if ((FindPlace = mainForm.MyRTBox .Text.IndexOf (txtToSearch.Text ,FindPlace))==-1)
					{
						MessageBox.Show ("没有搜索到!");
						FindPlace = 0;	//没有找到,重置查找位置为文本起点
					}
					else
					{	//选中找到的文本,使其明显
						mainForm.MyRTBox.Select (FindPlace,txtToSearch.Text .Length );
						//找到了,置查找位置为下一个位置
						FindPlace = FindPlace + txtToSearch.Text .Length ;
						mainForm.Activate ();
					}
				}
			}
		}
	}
}

⌨️ 快捷键说明

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