在richtextbox中对URL进行右键单击事件

时间:2012-10-01 19:09:07

标签: c# url richtextbox

我在C#中的richtextbox中有一个链接。当我在网址链接上,我想显示一个菜单。如果它不是网址,那么我不想显示任何内容。现在我正在点击鼠标按下事件并根据鼠标指针位置选择行,如果所选行是有效的URL,我会显示菜单。它工作得很好,但是当我在网址旁边有一些文字时,仍然会将该行检测为有效网址并显示菜单。此外,我也无法利用鼠标更改事件。关于如何完成我想要做的事情的任何想法?

谢谢,

1 个答案:

答案 0 :(得分:1)

不确定这是否是您要找的,但我相信您会找到比这更好的

您可以在调用MouseDown时尝试此操作

private void richTextBox1_MouseDown(object sender, MouseEventArgs e)
{
    // Continue if the Right mouse button was clicked
    if (e.Button == MouseButtons.Right)
    {
        // Check if the selected item starts with http://
        if (richTextBox1.SelectedText.IndexOf("http://") > -1)
        {
            // Avoid popping the menu if the value contains spaces
            if (richTextBox1.SelectedText.Contains(' '))
            {
               // Show the menu
               contextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y);
            }
        }   
    }
}

或者单击链接时,这不适用于鼠标右键单击

private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
     contextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y);
}

谢谢,

我希望这会有所帮助:)