当前位置: 首页 > news >正文

winfrom 查询某字符串 找到它在 richTextbox 的位置 定位 并高亮 并且滚动定位到所查询的字符串所在的行

如图:

代码:

 //查找关键字private void buttonSearch_Click(object sender, EventArgs e){string searchText = textBoxSearch.Text;if (!string.IsNullOrEmpty(searchText)){TextBoxFinds(txtJSON, searchText);TextBoxFinds(txtSQL, searchText);}}//查找关键字,关键字高亮,并且定位到关键字处public void TextBoxFinds(RichTextBox richTextBox, string searchText){try{// 清除之前的高亮显示richTextBox.SelectAll();richTextBox.SelectionBackColor = Color.White;richTextBox.SelectionColor = Color.Black;// 查找字符串int index = richTextBox.Find(searchText);if (index != -1){// 高亮显示找到的字符串richTextBox.Select(index, searchText.Length);richTextBox.SelectionBackColor = Color.Yellow;richTextBox.SelectionColor = Color.Black;// 获取找到的字符串所在的行int line = richTextBox.GetLineFromCharIndex(index);// 滚动到该行int firstCharIndex = richTextBox.GetFirstCharIndexFromLine(line);richTextBox.SelectionStart = firstCharIndex;richTextBox.ScrollToCaret();}}catch { }}

http://www.dtcms.com/a/136451.html

相关文章:

  • Meltdown原理介绍:用户空间读取内核内存
  • apt3.0和apt2.0的区别
  • AI数字人分身无限克隆,开启IP视频创作新纪元!
  • 如何判断一个js对象是否拥有某个属性(区分自身属性和原型链上的属性)?
  • [管理与领导-129]:向上管理-组织架构、股权架构、业务架构、流程架构,看每个人在组织中的位置和重要性
  • Angular 框架详解:从入门到进阶
  • 数据库主从复制
  • 解决Ubuntu终端命令不能补全的问题
  • Linux:解决 yum 官方源无法使用(CentOS 7)
  • postman使用技巧
  • Postman做自动化测试
  • opencv函数展示
  • 【数字图像处理】数字图像空间域增强(3)
  • WIFI扫描记录
  • Spark-SQL核心编程(二)(三)
  • LIB-ZC, 一个跨平台(Linux)平台通用C/C++扩展库, IO操作
  • 管理与维护samba服务器
  • 信创服务器-大国崛起,信创当道!
  • SpringBoot异常处理之自定义统一的错误处理页面
  • 大模型全景解析:从技术突破到行业变革
  • 专为路由器和嵌入式设备设计的OpenWrt是什么?
  • RabbitMQ架构原理及消息分发机制
  • STM32江科大----------PID算法
  • SpringBoot整合Redis限流
  • 数据中台进化史:从概念萌芽到价值变现的蜕变之路
  • 理解最左前缀原则:联合索引命中规则全解析(含流程图)
  • Flutter PIP 插件 ---- iOS Video Call 自定义PIP WINDOW渲染内容
  • 基于MATLAB与deepSeek-R1的控制系统分析工具开发与应用
  • 打通任督二脉 - Device Plugin 让 k8s “看见” GPU
  • Python Selenium 一小时速通教程