首页 > Windows开发 > 详细

WPF textBox 字符串搜索,滚动到当前行

时间:2017-04-13 19:13:24      阅读:417      评论:0      收藏:0      [点我收藏+]
 1 //执行事件 
 2 private void Button_Click(object sender, RoutedEventArgs e)
 3         {
 4 
 5             if (txtstr.Text == "")
 6             {
 7                 MessageBox.Show("查询字符串为空!");
 8                 return;
 9             }
10             CXtxt_string();
11         }
12 
13 //执行方法
14         int index = 0;//从第0个字符串开始查找
15         public void CXtxt_string()
16         {
17             string str = textBox1.Text;//设置需要查找的字符串
18             index = textBox2.Text.IndexOf(str , index);//返回str在textBox1中的索引位置
19             if (index < 0)//如果没有找不到字符串,则return
20             {
21                 index = 0;
22                 textBox2.SelectionStart = 0;
23                 textBox2.SelectionLength = 0;
24                 MessageBox.Show("已到结尾");
25                 return;
26             }
27             textBox2.SelectionStart = index;//设置需要选中字符串的开始位置
28             textBox2.SelectionLength = str.Length;//设置需要选中字符串的结束位置
29             index++;
30             textBox2.Focus();//设置焦点
31 
32             //int rows = textBox2.GetFirstVisibleLineIndex();//得到可见文本框中第一个字符的行索引
33             int line = textBox2.GetLineIndexFromCharacterIndex(index);//返回指定字符串索引所在的行号
34             //Debug.WriteLine(rows + ",," + line);
35             int lastline = textBox2.GetLastVisibleLineIndex();
36             if (line > lastline)
37             {
38                 textBox2.ScrollToLine(lastline+1);//滚动到视图中指定行索引
39             }
40         }
41     }
42 }

WPF 在textBox查找字符串并且自动滚动到字符串所在的行

WPF textBox 字符串搜索,滚动到当前行

原文:http://www.cnblogs.com/jingxuan-li/p/6705478.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!