Question

I have a RichTextBox in a WinForm with URL:s. They can be clicked, but I want to detect if the user right clicked them.

Was it helpful?

Solution

I think this is what you might be looking for. On the MouseDownEvent first check that you are dealing with a right click. Then figure out the clicked position and work your way back to the text.

    private void DoMouseDown(object sender, MouseEventArgs e)
    {

        if (e.Button == MouseButtons.Right)
        {
            RichTextBox rtb = (RichTextBox)sender;
            int charIndex = rtb.GetCharIndexFromPosition(new Point(e.X, e.Y));
            int lineIndex = rtb.GetLineFromCharIndex(charIndex);
            string clickedText = rtb.Lines[lineIndex];

            // now check if the text was indeed a link
            Regex re = new Regex("http://(www\\.)?([^\\.]+)\\.([^\\.]+)");
            bool isLink = re.IsMatch(s);
        }            
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top