How do I detect an alt click on link in RichTextBox
I have a RichTextBox in a WinForm with URL:s. They can be clicked, but I want to detec开发者_Go百科t if the user right clicked them.
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);
}
}
精彩评论