ICSharpCode.TextEditor设置选择的文本

翻遍了ICSharpCode.TextEditor源码,居然没找到选择指定文本的现成的方法,可能我还不熟悉源码或运气不好没找到相关方法,只好自己动手了。

///   <summary>
        
///  设置选择文本。
        
///   </summary>
        
///   <param name="offset"> 选择文本起始处的光标偏移量 </param>
        
///   <param name="length"> 要选择文本的长度 </param>
         public   void  SetSelection( int  offset,  int  length)
        {
            
// 将光标至于行首。
             this .CurrentTextEditor.ActiveTextAreaControl.Caret.Column  =   0 ;
            
// 根据当前光标所在位置确定是向上移动或向下移动。
             int  x  =  offset  >   this .CurrentTextEditor.ActiveTextAreaControl.Caret.Offset  ?   1  :  - 1 ;
            
// 首先将光标以行为单位进行上下移动。
             while  ( true )
            {
                
// 记录上一次的行号。
                 int  preLine  =   this .CurrentTextEditor.ActiveTextAreaControl.Caret.Line;
                
// 行号加或减1。
                 this .CurrentTextEditor.ActiveTextAreaControl.Caret.Line  +=   1   *  x;
                
// 如果行号没变,表示已经不能上下移动,跳出。
                 if  ( this .CurrentTextEditor.ActiveTextAreaControl.Caret.Line  ==  preLine)
                    
break ;

                
if  (x  >   0 )
                {
                    
// 向下移动且已到到位或超过目标位置,则向上回退一行,以便下面以字符为单位统一向下移动。
                     if  ( this .CurrentTextEditor.ActiveTextAreaControl.Caret.Offset  >=  offset)
                    {
                        
this .CurrentTextEditor.ActiveTextAreaControl.Caret.Line  -=   1 ;
                        
break ;
                    }
                }
                
else
                {
                    
// 向下移动且已到到位或超过目标位置则跳出循环。
                     if  ( this .CurrentTextEditor.ActiveTextAreaControl.Caret.Offset  <=  offset)
                        
break ;
                }
            }

            
// 以字符为单位向下移动光标以到达指定位置。
             while  ( this .CurrentTextEditor.ActiveTextAreaControl.Caret.Offset  !=  offset)
            {
                
// 记录上一次的位置。
                 int  preOffset  =   this .CurrentTextEditor.ActiveTextAreaControl.Caret.Offset;
                
this .CurrentTextEditor.ActiveTextAreaControl.Caret.Column  +=   1 ;

                
// 如果到达行尾则向下跳一行。
                 if  ( this .CurrentTextEditor.ActiveTextAreaControl.Caret.Offset  ==  preOffset)
                {
                    
this .CurrentTextEditor.ActiveTextAreaControl.Caret.Line  +=   1 ;
                    
this .CurrentTextEditor.ActiveTextAreaControl.Caret.Column  =   0 ;
                }
            }
            
            
// 获取当前坐标。
            Point locationStart  =   this .CurrentTextEditor.ActiveTextAreaControl.Caret.Position;

            
// 计算将要选择的量。
            offset  +=  length;
            offset 
=  Math.Min(offset,  this .CurrentTextEditor.Text.Length);
            offset 
=  Math.Max(offset,  0 );
            
this .CurrentTextEditor.ActiveTextAreaControl.Caret.Column  =   0 ;

            
// 将光标以行为单位进行向下移动。
             while  ( true )
            {
                
int  preLine  =   this .CurrentTextEditor.ActiveTextAreaControl.Caret.Line;
                
this .CurrentTextEditor.ActiveTextAreaControl.Caret.Line  +=   1  ;
                
if  ( this .CurrentTextEditor.ActiveTextAreaControl.Caret.Line  ==  preLine)
                    
break ;

                
if  ( this .CurrentTextEditor.ActiveTextAreaControl.Caret.Offset  >=  offset)
                {
                    
this .CurrentTextEditor.ActiveTextAreaControl.Caret.Line  -=   1 ;
                    
break ;
                }
            }

            
// 以字符为单位向下移动光标以到达指定位置。
             while  ( this .CurrentTextEditor.ActiveTextAreaControl.Caret.Offset  !=  offset)
            {
                
int  preOffset  =   this .CurrentTextEditor.ActiveTextAreaControl.Caret.Offset;
                
this .CurrentTextEditor.ActiveTextAreaControl.Caret.Column  +=   1 ;
                
if  ( this .CurrentTextEditor.ActiveTextAreaControl.Caret.Offset  ==  preOffset)
                {
                    
this .CurrentTextEditor.ActiveTextAreaControl.Caret.Line  +=   1 ;
                    
this .CurrentTextEditor.ActiveTextAreaControl.Caret.Column  =   0 ;
                }
            }

            
// 获取当前坐标。
            Point locationEnd  =   this .CurrentTextEditor.ActiveTextAreaControl.Caret.Position;
            
// 设置选择的文本。
             this .CurrentTextEditor.ActiveTextAreaControl.TextArea.SelectionManager.SetSelection(locationStart, locationEnd);
            
this .CurrentTextEditor.ActiveTextAreaControl.TextArea.ScrollToCaret();
        }       
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值