当我选择文本时,我找不到确定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;
}还有其他的方法吗?
发布于 2011-11-26 19:26:51
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

,
。
https://stackoverflow.com/questions/8277027
复制相似问题