📄 frmfind.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace Example_2
{
/// <summary>
/// Summary description for frmFind
/// </summary>
public class frmrichText : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Button btnFind;
private System.Windows.Forms.TextBox txtFind;
private System.Windows.Forms.Label lblFind;
private System.Windows.Forms.RichTextBox rtbText;
private static int count;
private string str="";
private string searchstr;
private int start;
private int location;
private System.Windows.Forms.Label lblFileName;
private System.Windows.Forms.TextBox txtFileName;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmrichText()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.rtbText = new System.Windows.Forms.RichTextBox();
this.btnLoad = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.btnFind = new System.Windows.Forms.Button();
this.txtFind = new System.Windows.Forms.TextBox();
this.lblFind = new System.Windows.Forms.Label();
this.lblFileName = new System.Windows.Forms.Label();
this.txtFileName = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// rtbText
//
this.rtbText.Location = new System.Drawing.Point(48, 69);
this.rtbText.Name = "rtbText";
this.rtbText.Size = new System.Drawing.Size(326, 103);
this.rtbText.TabIndex = 0;
this.rtbText.Text = "";
//
// btnLoad
//
this.btnLoad.Location = new System.Drawing.Point(288, 17);
this.btnLoad.Name = "btnLoad";
this.btnLoad.Size = new System.Drawing.Size(90, 25);
this.btnLoad.TabIndex = 1;
this.btnLoad.Text = "加载文件(&L)";
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(288, 224);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(90, 25);
this.btnExit.TabIndex = 2;
this.btnExit.Text = "退出(&E)";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// btnFind
//
this.btnFind.Location = new System.Drawing.Point(182, 224);
this.btnFind.Name = "btnFind";
this.btnFind.Size = new System.Drawing.Size(90, 25);
this.btnFind.TabIndex = 3;
this.btnFind.Text = "查找文本(&F)";
this.btnFind.Click += new System.EventHandler(this.btnFind_Click);
//
// txtFind
//
this.txtFind.Location = new System.Drawing.Point(211, 190);
this.txtFind.Name = "txtFind";
this.txtFind.Size = new System.Drawing.Size(163, 21);
this.txtFind.TabIndex = 4;
this.txtFind.Text = "";
//
// lblFind
//
this.lblFind.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.lblFind.Location = new System.Drawing.Point(96, 190);
this.lblFind.Name = "lblFind";
this.lblFind.Size = new System.Drawing.Size(106, 17);
this.lblFind.TabIndex = 5;
this.lblFind.Text = "要查找的文本:";
//
// lblFileName
//
this.lblFileName.Location = new System.Drawing.Point(10, 17);
this.lblFileName.Name = "lblFileName";
this.lblFileName.Size = new System.Drawing.Size(115, 25);
this.lblFileName.TabIndex = 6;
this.lblFileName.Text = "输入文件名:";
//
// txtFileName
//
this.txtFileName.Location = new System.Drawing.Point(144, 17);
this.txtFileName.Name = "txtFileName";
this.txtFileName.Size = new System.Drawing.Size(134, 21);
this.txtFileName.TabIndex = 7;
this.txtFileName.Text = "";
//
// frmrichText
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(384, 272);
this.Controls.Add(this.txtFileName);
this.Controls.Add(this.lblFileName);
this.Controls.Add(this.lblFind);
this.Controls.Add(this.txtFind);
this.Controls.Add(this.btnFind);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnLoad);
this.Controls.Add(this.rtbText);
this.Name = "frmrichText";
this.Text = "查找";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmrichText());
}
private void btnLoad_Click(object sender, System.EventArgs e)
{
try
{
rtbText.LoadFile(txtFileName.Text);
}
catch(FileNotFoundException fexcep)
{
MessageBox.Show (fexcep.Message);
}
}
private void btnExit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void btnFind_Click(object sender, System.EventArgs e)
{
searchstr = txtFind.Text.Trim ();
//if find is being done first time
if (count == 0)
{
str = rtbText.Text.Trim() ;
start = 0;
}
else //if find has been pressed more than once
{
//assign the new location to start searching from
start = location + searchstr.Length;
str = str.Trim();
if (start<= str.Length)
str = str.Substring(start);
}
count++;
//call the function
location = find(searchstr,start);
//mark the text that was found
this.rtbText.SelectionStart = location;
this.rtbText.SelectionLength = txtFind.Text.Length;
this.rtbText.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
this.rtbText.SelectionColor = Color.Red;
}
private int find(string searchstr, int start)
{
int loc = this.rtbText.Find(searchstr,start,rtbText.Text.Length,RichTextBoxFinds.WholeWord);
return loc;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -