翻遍了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();
}
/// 设置选择文本。
/// </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();
}