CodeRunner
CodeRunner

Reputation: 391

right click event on URLs in richtext box

I have a link in a richtextbox in C#. When I right on a url link, I want to show a menu. if it is not a url, then I do not want to show anything. Right now I am tapping into the mouse down event and selecting the line based on the mouse pointer position and if the selected line is a valid url, I show the menu. it works great, but when I have some text next to the url, still the line is detected as a valid url and the menu appears. Also I am unable to tap into the mouse changed event as well. Any ideas on how I could accomplish what I am trying to do?

Thanks,

Upvotes: 0

Views: 483

Answers (1)

Picrofo Software
Picrofo Software

Reputation: 5571

Not sure if this is what you are looking for, but I believe that you'll find better than this

You may try this when the MouseDown is called

private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
    // Continue if the Right mouse button was clicked
    if (e.Button == MouseButtons.Right)
    {
        // Check if the selected item starts with http://
        if (richTextBox1.SelectedText.IndexOf("http://") > -1)
        {
            // Avoid popping the menu if the value contains spaces
            if (richTextBox1.SelectedText.Contains(' '))
            {
               // Show the menu
               contextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y);
            }
        }   
    }
}

Or this when a Link is Clicked, but this won't apply for Mouse Right-Click

private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
     contextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y);
}

Thanks,

I hope this helps :)

Upvotes: 1

Related Questions