[英]RichTextBox and Caret Position
我选择文字时,找不到在RTB中确定插入符号位置的方法。 SelectionStart
不是一个选项 。
我想检测选择的方向是向后还是向前。 我正在尝试在SelectionChanged
事件中实现这一点。 任何提示将不胜感激。
编辑:
我通过使用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;
}
还有什么其他方法可以做到?
SelectionStart和SelectionLength属性在左侧选择期间更改,SelectionLength属性在右侧选择期间更改。
简单的解决方案:
int tempStart;
int tempLength;
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
if (richTextBox1.SelectionType != RichTextBoxSelectionTypes.Empty)
{
if (richTextBox1.SelectionStart != tempStart)
lblSelectionDesc.Text = "Left" + "\n";
else if( richTextBox1.SelectionLength != tempLength)
lblSelectionDesc.Text = "Right" + "\n";
}
else
{
lblSelectionDesc.Text = "Empty" + "\n";
}
tempStart = richTextBox1.SelectionStart;
tempLength = richTextBox1.SelectionLength;
lblSelectionDesc.Text += "Start: " + richTextBox1.SelectionStart.ToString() + "\n";
lblSelectionDesc.Text += "Length: " + richTextBox1.SelectionLength.ToString() + "\n";
}
控制项:
RitchTextBox + 2xLabels
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.