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.
+1
A:
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);
}
}
cagreen
2009-10-29 02:04:08
That will give me the text of the line, but I won't know if it's a link or something else on that line that was clicked. I could 'walk' the characters around the clicked position, but that is the kind of code I had hope to not write when using the built in functionality of the RichTextBox.
idstam
2009-10-29 08:38:59
+1 for trying though :)
idstam
2009-10-29 08:39:32
Why not just use regex to see is it's a link?Regex re = new Regex("http://(www\\.)?([^\\.]+)\\.([^\\.]+)");bool isLink = re.IsMatch(clickedText);Am I getting closer to what you are after?
cagreen
2009-10-29 12:37:12
You still only see if the line you clicked contains a link not if you actually clicked the link. I'll just use the CharIndex and check if I'm actually ON the link.
idstam
2009-10-30 06:37:29