2
我有一个WinForm中的RichTextBox与URL:s。 他们可以点击,但我想检测用户是否右键单击它们。如何检测RichTextBox中的链接ALT按钮
我有一个WinForm中的RichTextBox与URL:s。 他们可以点击,但我想检测用户是否右键单击它们。如何检测RichTextBox中的链接ALT按钮
我认为这是你可能要找的。在MouseDownEvent上,首先检查您是否正在处理右键单击。然后找出被点击的位置并回到文本中。
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);
}
}
这将给我的行文本,但我不知道它是否是链接或点击该行上的其他内容。我可以围绕点击位置“走动”角色,但这是我希望在使用RichTextBox的内置功能时不能写入的代码类型。 – idstam 2009-10-29 08:38:59
+1虽然尝试:) – idstam 2009-10-29 08:39:32
为什么不只是使用正则表达式来看它是一个链接? Regex re = new Regex(“http://(www \\。)?([^ \\。] +)\\。([^ \\。] +)”); bool isLink = re.IsMatch(clickedText); 我越来越接近你以后的样子吗? – cagreen 2009-10-29 12:37:12