Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Moving Cursor in RichTextBox on Right-Click

I have a RichTextBox control. When you left-click in the text the cursor jumps to where you clicked. I want this to happen when I right-click as well. I'm not sure how to do this. Thanks!

like image 440
morrie Avatar asked Jun 08 '10 10:06

morrie


1 Answers

Assuming winforms:

Implement a MouseUp event handler like so:

private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        RichTextBox box = (RichTextBox)sender;
        box.SelectionStart = box.GetCharIndexFromPosition(e.Location);
        box.SelectionLength = 0;
    }
}
like image 138
Phil Gan Avatar answered Sep 19 '22 19:09

Phil Gan