كيف يمكنني الكشف عن النقر على وصلة بديل في RichTextBox
-
07-07-2019 - |
سؤال
ولدي RichTextBox في WinForm مع URL: ق. ويمكن النقر عليها، ولكن أريد أن كشف إذا كان المستخدم بالنقر فوق حق لهم.
المحلول
وأعتقد أن هذا هو ما كنت قد تكون تبحث عنه. على 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);
}
}
لا تنتمي إلى StackOverflow