我需要做一个像记事本功能的软件,其他都做差不多了,就是查找功能,需要的查找功能和记事本有些区别,记事本是一个一个往下或者向上查找,我要的功能是全文中包含指定字词的全部选中.谁能提供些思路或者有代码,谢了.
private void btn_Search_Click(object sender, EventArgs e) //搜索
{
string searchTxt = this.txt_Search.Text; //txt_Search为你查询时输入查询内容的文本框
int txtLength = searchTxt.Length;
if (!string.IsNullOrEmpty(searchTxt))
{
richTextBox1.SelectAll();
richTextBox1.SelectionBackColor = Color.White;
richTextBox1.SelectionFont = new Font("宋体", 12);
int searchTxtLength = richTextBox1.Text.Length;
for (int i = 0; i < searchTxtLength; i++)
{
if (richTextBox1.Text[i] == searchTxt[0])
{
string searResult = string.Empty;
searResult += searchTxt[0];
for (int j = 1; j < txtLength; j++)
{
if (this.richTextBox1.Text[i + j] == searchTxt[j])
{
searResult += searchTxt[j];
}
}
if (searResult.Length == txtLength)
{
richTextBox1.Select(i, txtLength);
richTextBox1.SelectionBackColor = Color.White;
richTextBox1.SelectionFont = new Font("宋体",12, FontStyle.Bold); //设置想要的样式
}
}
}
txt_Search.Focus();
}
else
{
MessageBox.Show("请输入查询内容!");
}
}
private void btn_Search_Click(object sender, EventArgs e) //搜索
{
string searchTxt = this.txt_Search.Text; //txt_Search为你查询时输入查询内容的文本框
int txtLength = searchTxt.Length;
if (!string.IsNullOrEmpty(searchTxt))
{
richTextBox1.SelectAll();
richTextBox1.SelectionBackColor = Color.White;
richTextBox1.SelectionFont = new Font("宋体", 12);
int searchTxtLength = richTextBox1.Text.Length;
for (int i = 0; i < searchTxtLength; i++)
{
if (richTextBox1.Text[i] == searchTxt[0])
{
string searResult = string.Empty;
searResult += searchTxt[0];
for (int j = 1; j < txtLength; j++)
{
if (this.richTextBox1.Text[i + j] == searchTxt[j])
{
searResult += searchTxt[j];
}
}
if (searResult.Length == txtLength)
{
richTextBox1.Select(i, txtLength);
richTextBox1.SelectionBackColor = Color.White;
richTextBox1.SelectionFont = new Font("宋体",12, FontStyle.Bold); //设置想要的样式
}
}
}
txt_Search.Focus();
}
else
{
MessageBox.Show("请输入查询内容!");
}
}
我想应该是这样的,不过"txt_Search.Focus();"获取焦点不对,怎么改呢?还望指教!谢了