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?
SelectionStart and SelectionLength properties changes during left side selection and SelectionLength changes during right side selection.
Simple solution:
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";
}
Controls:
RitchTextBox + 2xLabels
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.