最近都在做一个能在Winform的RichTextBox中添加像网页中的超链接,点击后自动解析选项关键词弹出列表框,显示候选词,实现快速录入的功能.如下图所示

richTextBox插入链接的方法

  /// <summary>/// Insert a given text as a link into the RichTextBox at the current insert position./// </summary>/// <param name="text">Text to be inserted</param>public void InsertLink(string text){InsertLink(text, this.SelectionStart);}/// <summary>/// Insert a given text at a given position as a link. /// </summary>/// <param name="text">Text to be inserted</param>/// <param name="position">Insert position</param>public void InsertLink(string text, int position){if (position < 0 || position > this.Text.Length)throw new ArgumentOutOfRangeException("position");this.SelectionStart = position;this.SelectedText = text;this.Select(position, text.Length);this.SetSelectionLink(true);this.Select(position + text.Length, 0);}/// <summary>/// Insert a given text at at the current input position as a link./// The link text is followed by a hash (#) and the given hyperlink text, both of/// them invisible./// When clicked on, the whole link text and hyperlink string are given in the/// LinkClickedEventArgs./// </summary>/// <param name="text">Text to be inserted</param>/// <param name="hyperlink">Invisible hyperlink string to be inserted</param>public void InsertLink(string text, string hyperlink){InsertLink(text, hyperlink, this.SelectionStart);}/// <summary>/// Insert a given text at a given position as a link. The link text is followed by/// a hash (#) and the given hyperlink text, both of them invisible./// When clicked on, the whole link text and hyperlink string are given in the/// LinkClickedEventArgs./// </summary>/// <param name="text">Text to be inserted</param>/// <param name="hyperlink">Invisible hyperlink string to be inserted</param>/// <param name="position">Insert position</param>public void InsertLink(string text, string hyperlink, int position){try{if (position < 0 || position > this.Text.Length)throw new ArgumentOutOfRangeException("position");this.SelectionStart = position;//this.SelectedRtf = @"{\rtf1\ansi " + text + @"\v #" + hyperlink + @"\v0}";this.SelectedRtf = @"{\rtf1\ansicpg936" + text + @"\v #" + hyperlink + @"\v0}";  //解决中文乱码this.Select(position, text.Length + hyperlink.Length + 1);this.SetSelectionLink(true);this.Select(position + text.Length + hyperlink.Length + 1, 0);}catch (Exception ex){}}

链接单击事件的方法

 /// <summary>/// 链接点击事件/// </summary>/// <param name="e"></param>protected override void OnLinkClicked(LinkClickedEventArgs e){string linkModKeyName = GetLinkedModId(e.LinkText);string linkDispStr =  GetLinkedDisplayString(e.LinkText);SetSelectionLink(false);// GetXY();int linkstrSart =  GetLeftLinkStartPostion(linkDispStr, iMouseIndex);this.LastSelectionSart = linkstrSart;this.LastSelectionLength = linkDispStr.Length;this.SelectionStart = linkstrSart;this.SelectionLength = linkDispStr.Length;Point mPoint = Control.MousePosition;ItemListFrm ilf = new ItemListFrm( cmmsp, linkModKeyName, mPoint);ilf.Event_AfterSelectedItems += AfterChangeHouXuanCiReSetLink;ilf.Show();base.OnLinkClicked(e);}

候选词窗口关闭事件

  /// <summary>/// 选词框关闭后操作/// </summary>/// <param name="oKeyName"></param>/// <param name="oDisplayName"></param>private void AfterChangeHouXuanCiReSetLink(string oKeyName, string oDisplayName){this.EditLink(oDisplayName, oKeyName, this.LastSelectionSart, this.LastSelectionLength); }

修改选中的词,并修改超链接

  public void EditLink(string text,string hyperlink,int position,  int oldLength){try{if (position < 0 || position > this.Text.Length)throw new ArgumentOutOfRangeException("position");this.SelectionStart = position;this.SelectionLength = oldLength;string tmpstr = this.SelectedText;this.SetSelectionLink(false);this.SelectionStart = position;this.SelectionLength = oldLength;this.SelectedText = text;//this.SelectionStart = position;//this.SelectionLength = oldLength;//this.SelectedText = "";this.SelectionStart = position;this.SelectionLength = text.Length;this.SelectedRtf = @"{\rtf1\ansicpg936" + text + @"\v #" + hyperlink + @"\v0}";  //解决中文乱码this.Select(position, text.Length + hyperlink.Length + 1);this.SetSelectionLink(true);this.Select(position + text.Length + hyperlink.Length + 1, 0);}catch (Exception ex){ex.Message.ToString();}}

演示下载地址: https://download.csdn.net/download/spd260/10278920

RichTextBox超链接解析相关推荐

  1. Unity Text 插入超链接

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 当前测试 ...

  2. Unity超链接:支持点击事件,下划线以及自定义颜色

    基于这篇: zyf2533 - Unity 超链接 Text 修正了一些bug,额外支持了下划线以及自定义颜色. /*https://blog.csdn.net/zyf2533/article/det ...

  3. Unity UGUI图文混排(六) -- 超链接

    图文混排更新到超链接这儿,好像也差不多了,不过就在最后一点,博主也表现得相当不专业,直接整合了山中双木林同学提供的超链接的解决方案,博主甚至没来得及细看就直接复制了,但感觉还是挺好用的. 博主已经将超 ...

  4. C# RichTextBox的用法

    C# RichTextBox的用法 2021-03-15  Windows程序  RichTextBox是一种可用于显示.输入和操作格式文本,除了可以实现TextBox的所有功能,还能提供富文本的显示 ...

  5. Unity Text 实现图文混排和超链接功能

    unity 已经在2021版本使用了全面使用了TMP实现图文混排和超链接这两个功能.由于当前项目并没有使用TMP.所以需要基于Text实现这两个功能,但是在2019之后的版本,Text 生成顶点时不再 ...

  6. 基于HtmlParser的网络爬虫

    一. 目标 获取网页中的超链接及链接名,如从http://www.hao123.com/开始,抓取所有hao123链接到的超链接,再以获取到的链接网页为目标,获取它所链接到的网页. 二.环境及开发工具 ...

  7. 1.html5+css3基础学习笔记(上)

    1 HTML简介 1.1 W3C标准 伯纳斯李1994年建立万维网联盟( W3C ),W3C的出现为了制订网页开发的标准,以使同一个网页在不同的浏览器中有相同的效果.所以,我们需要制订我们编写的网页都 ...

  8. RDP报表工具全新发布V2.0

    RDP报表工具是一款轻量级的企业Web报表工具,自带Web版的类Excel设计器,容易上手,使用简单,仅需简单拖拽,就可以制作出各种实用的报表. 免费使用不收取任何费用. 官网:product.mft ...

  9. Python解析html获取超链接地址并下载解析

    安装BeautifulSoup pip install beautifulsoup4 解析html,获取超链接 from bs4 import BeautifulSoup # 使用文档解析类库,解析H ...

最新文章

  1. APACHE服务器出现No input file specified.的完美解决方案
  2. html5,用或不用,它都在那里
  3. const C++ 用法总结
  4. ArcGIS Server 开发系列(二)--Web ADF 编程
  5. 5.企业安全建设入门(基于开源软件打造企业网络安全) --- 业务安全
  6. 服务器监视Zabbix 5.0 - Window Agent安装
  7. java war 反编译_war反编译成java项目
  8. 遥感影像数据下载网站整理
  9. c语言从入门到放弃(初识)
  10. bootstrap日期插件的使用
  11. 22考研英语高频词汇
  12. 《WEB开发-阿里云建站》第1章 建站前的准备
  13. 他,1年9个月获清华博士学位,一作身份发27篇SCI,组队击败NASA打破“航天奥林匹克”欧美垄断...
  14. [办公软件word] 怎么让Worde2019显示所有批注?
  15. 如何处理u盘一插进电脑就自动打开里面的所有文件夹
  16. 学习数学:往日油印稿,今日电子书
  17. [Debug] 法语输入
  18. IntelliJ IDEA(社区版) 背景图片、颜色、字体等设置
  19. 树莓派系统剪裁、克隆
  20. Vivado FPGA基础设计操作流程(1)

热门文章

  1. ClickHouse ru.yandex.clickhouse.except.ClickHouseException: ClickHouse exception, code: 210, host:
  2. 【Java】K-means算法Java实现以及图像分割
  3. ftrace 的使用---更新中
  4. JAVA —— Docker
  5. 关于simpleCD
  6. Lisp 的本质(2)
  7. 分享活动:中国移动通信研究院和答题吧网站合作的调研活动,答题送积分,30个积分即可兑换30元话费。...
  8. Flash远程调用Red5方法
  9. 台式计算机用电量是多少,电脑一天耗电量是多少(含台式机和笔记本)?
  10. jquery中before、insertBefore、after、insertAfter、append、appendTo用法解析