ICSharpCode.TextEditor设置选择的文本
来源:互联网 发布:众病之王癌症传 知乎 编辑:程序博客网 时间:2024/05/19 05:39
翻遍了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();
}
- ICSharpCode.TextEditor设置选择的文本
- 使用ICSharpCode.TextEditor制作一个语法高亮显示的XML编辑器
- 语法高亮显示控件ICSharpCode.TextEditor
- 爱上语法高亮控件ICSharpCode.TextEditor ~o~
- ICSharpCode.TextEditor扩展——增加右键菜单
- TextEditor的基本操作
- 精简版的textEditor
- c#中用ICSharpCode.SharpZipLib实现文本压缩
- 在点击TextEditor编辑框后,默认是选中编辑框内的全部内容,去掉默认选择内容这个效果
- 文本编辑器的选择
- ubuntu的texteditor不能显示中文
- ICSharpCode.SharpZLib的使用记录
- 设置鼠标经过时自动选择文本
- 常规开发设置 -> 文本选择快捷键
- 怎样设置JTextArea 文本域中选中的文字的颜色和选择区域背景颜色
- 文本的特征属性选择
- 文本域的设置
- HTML的文本设置
- android学习点缀
- 在线制作gif
- [Windows 7 安全新功能] Windows 7 应用程序控制策略:深度安全防御
- flash电视站点
- monitor
- ICSharpCode.TextEditor设置选择的文本
- Dual channel/Stereo/Joint stereo 区别
- SUID、SGID
- PHP 计算页面加载时间
- su:鉴定故障
- 测试的目的应该是验证需求
- [Windows 7 安全新功能] Windows 7 的指纹识别:简约而不简单
- 链接器都干了些什么?
- 标签的value值动态赋值