RichTextBox 和插入符位置
当我选择文本时,我找不到确定 RTB 中插入符号位置的方法。 SelectionStart
不是一个选项。
我想检测选择的方向是向后还是向前。我试图在 SelectionChanged
event 中实现这一目标。任何提示将不胜感激。
编辑:
我通过使用 mouseDown 和 mouseUp 事件注册鼠标移动方向(X 轴)来解决这个问题。
代码:
bool IsMouseButtonPushed = false;
int selectionXPosition = 0, sDirection=0;
private void richTextBox_SelectionChanged(object sender, EventArgs e)
{
if (sDirection==2)//forward
{
//dosomething
}
}
private void richTextBox_MouseMove(object sender, MouseEventArgs e)
{
if (IsMouseButtonPushed && (selectionXPosition - e.X) > 0)//backward
{
sDirection = 1;
}
else if (IsMouseButtonPushed && (selectionXPosition - e.X) < 0)//forward
{
sDirection = 2;
}
}
private void richTextBox_MouseDown(object sender, MouseEventArgs e)
{
IsMouseButtonPushed = true;
selectionXPosition = e.X;
}
private void richTextBox_MouseUp(object sender, MouseEventArgs e)
{
IsMouseButtonPushed = false;
}
还有哪些其他方法可以做到这一点?
I can't find a way to determine the caret position in the RTB while i'm selecting text. SelectionStart
is not an option.
I want to detect the direction of selection whether its backward or forward. I'm trying to achieve this in SelectionChanged
event. Any tips would be appreciated.
EDIT:
I solved it by registering mouse movement direction (X axis) with mouseDown and mouseUp events.
Code:
bool IsMouseButtonPushed = false;
int selectionXPosition = 0, sDirection=0;
private void richTextBox_SelectionChanged(object sender, EventArgs e)
{
if (sDirection==2)//forward
{
//dosomething
}
}
private void richTextBox_MouseMove(object sender, MouseEventArgs e)
{
if (IsMouseButtonPushed && (selectionXPosition - e.X) > 0)//backward
{
sDirection = 1;
}
else if (IsMouseButtonPushed && (selectionXPosition - e.X) < 0)//forward
{
sDirection = 2;
}
}
private void richTextBox_MouseDown(object sender, MouseEventArgs e)
{
IsMouseButtonPushed = true;
selectionXPosition = e.X;
}
private void richTextBox_MouseUp(object sender, MouseEventArgs e)
{
IsMouseButtonPushed = false;
}
What are other ways to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SelectionStart 和 SelectionLength 属性在左侧选择期间发生变化,而 SelectionLength 属性在右侧选择期间发生变化。
简单的解决方案:
控件:
RitchTextBox + 2xLabels
SelectionStart and SelectionLength properties changes during left side selection and SelectionLength changes during right side selection.
Simple solution:
Controls:
RitchTextBox + 2xLabels