RichTextBox超链接解析

最近都在做一个能在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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值