using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Country_Char
{
public partial class focus : Form
{
public focus()
{
InitializeComponent();
}
private int _searchIndex = 0;//查找开始位置
private void button1_Click(object sender, EventArgs e)
{
var findWord = textBox1.Text.Trim();
if (string.IsNullOrEmpty(findWord) || string.IsNullOrEmpty(textBox1.Text.Trim())) return;
var searchResult = richTextBox1.Find(findWord, _searchIndex, RichTextBoxFinds.None);
if (searchResult == -1)
{
if (_searchIndex == 0)
{
/* MessageBoxTimeOut
* 自定义的延时自动关闭提示,可以用 MessageBox提示用户
*/
//new MessageBoxTimeOut().Show(1000, @"未找到相关内容!", @"提示", MessageBoxButtons.OK,
// MessageBoxIcon.Information);
}
/* _searchIndex>0
* 说明之前有查找结果,并且已经越过最后一个查找结果位置后,则自动从头开始查找。
* 也可以提示用户是否从头查找。
*/
_searchIndex = 0;
}
else
{
richTextBox1.Focus();
richTextBox1.Select(searchResult, findWord.Length);
_searchIndex = searchResult + findWord.Length;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
_searchIndex = 0;
}
}
}