模仿Word中的查找

View Code
1 private int FindPlace = 0; //文本查找位置,缺省为文本起点
2 private void btnStart_Click(object sender, EventArgs e)
3 {
4 if (txtToSearch.Text != "") //当查找内容不为空时,进行查找
5 {
6 //获得Form1窗体的引用
7 MainForm mainForm = (MainForm)this.Owner;
8 //如果主窗体文本内容不为空,进行查找
9 if (mainForm.rtbData.Text.Length > 0)
10 {
11 if ((FindPlace = mainForm.rtbData.Text.IndexOf(txtToSearch.Text, FindPlace)) == -1)
12 {
13 MessageBox.Show("没有搜索到!");
14 FindPlace = 0; //没有找到,重置查找位置为文本起点
15 }
16 else
17 { //选中找到的文本,使其明显
18 mainForm.rtbData.Select(FindPlace, txtToSearch.Text.Length);
19 //找到了,置查找位置为下一个位置
20 FindPlace = FindPlace + txtToSearch.Text.Length;
21 mainForm.Focus();
22 mainForm.Activate();
23 }
24 }
25 }
26 }

posted on 2011-04-14 16:53  源远流长  阅读(151)  评论(0编辑  收藏  举报

导航