Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
In a webControl, when the mouse cursor is "Hand" (usually over links,images), I want a custom contextMenu to be on or off. I tried to make something but is not working [the ContextMenuStrip is always showing,when I right click]. Why is this happening?
here is the code:
C#
public Form1()
{
    InitializeComponent();
    //I already set this in properties - showing here for clarity
    webBrowser1.IsWebBrowserContextMenuEnabled = false;
    webBrowser1.ContextMenuStrip = contextMenuStrip1;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (this.Cursor == Cursors.Hand) contextMenuStrip1.Visible = true;
    else contextMenuStrip1.Visible = false;
}


I suppose I must create a new event for Cursor...but is not working either. I don't really know how to do it right. Im not expert in making events. I don't understand them fully.
C#
public Form1()
        {
            InitializeComponent();
            CursorChanged += new EventHandler(Form1_CursorChanged);
        }
void Form1_CursorChanged(object sender, EventArgs e)
       {
            if (this.Cursor == Cursors.Hand) contextMenuStrip1.Visible = true;
            else contextMenuStrip1.Visible = false;
       }

---------------------
I see.At least can you write me what can I do?
Write a pseudocode or just describe what to do if its lot to do.
Thanks
Posted
Updated 7-Feb-12 10:37am
v13
Comments
_Q12_ 7-Feb-12 6:26am    
Thanks Slacker007 for reformulating. Much better now. ;) i have no idea that is possible, but I dont mind.
LanFanNinja 7-Feb-12 14:06pm    
Check my solution.
LanFanNinja 7-Feb-12 14:17pm    
If you have checked my solution before reading this comment check it again as I have made a small modification.
_Q12_ 7-Feb-12 14:26pm    
the "webBrowser1.ContextMenuStrip = contextMenuStrip1;" was set in properties...Is working with both variants.
LanFanNinja 7-Feb-12 19:41pm    
I have posted a new method(/// METHOD 2 ///) of doing this in my solution.

It is much simpler and does not seem to have the problems with Google search that the other method(/// METHOD 1 ///) has. However it has a problem! It does not work for images that are also links. :( And so far I have not found another solution that does.

Anyway I thought I would post it and maybe it will be of some use to you.

P.S. If I come up with a solution that does work for images that are also links I will post it.

Check this out it will do what you are wanting to do. I cannot guarantee this code in anyway though because I have never done this before. I just came up with this messing around. :) Let me know if it works for you.

/// METHOD 3 ///
Here is my last and final method I could come up with for doing this.
For the little testing I done it seems to work for everything!
I am not sure about performance though for that I guess only time will tell.
I actually used your idea to make use of a timer.
The timer updates all of the links MouseDown events every 100 milliseconds (you can try experimenting with higher values to possibly improve performance if needed) so I am not sure how this will affect the performance of your app at least with websites that have lots of links.
Anyway like I said it seems to work fine with everything I tried (Google instant predictions as well).

Let me know how it works for you.

C#
public partial class Form1 : Form
{
    Timer updateLinksTimer;

    public Form1()
    {
        InitializeComponent();

        updateLinksTimer = new Timer();
        // 100 = 100ms you can experiment with different values
        updateLinksTimer.Interval = 100;
        updateLinksTimer.Enabled = true;
        updateLinksTimer.Tick += new EventHandler(updateLinksTimer_Tick);

        webBrowser1.IsWebBrowserContextMenuEnabled = false;
        webBrowser1.ContextMenuStrip = contextMenuStrip_Text;

        contextMenuStrip_Links.Closed +=
            new ToolStripDropDownClosedEventHandler(contextMenuStrip_Links_Closed);

        webBrowser1.Navigate("google.com");
    }

    void contextMenuStrip_Links_Closed(object sender, ToolStripDropDownClosedEventArgs e)
    {
        webBrowser1.ContextMenuStrip = contextMenuStrip_Text;
    }

    void DocumentLink_MouseDown(object sender, HtmlElementEventArgs e)
    {
        webBrowser1.ContextMenuStrip = contextMenuStrip_Links;
    }

    private void updateLinksTimer_Tick(object sender, EventArgs e)
    {
        if (webBrowser1.Document != null)
        {
            for (int i = 0; i < webBrowser1.Document.Links.Count; i++)
            {
                webBrowser1.Document.Links[i].MouseDown -= DocumentLink_MouseDown;
                webBrowser1.Document.Links[i].MouseDown +=
                    new HtmlElementEventHandler(DocumentLink_MouseDown);
            }
        }
    }
}




/// METHOD 2 ///
NOTE: So far I cannot get this method to work with images that are also links! However it is much simpler and seems to not have the problems with google search that the other method has.

C#
public Form1()
{
    InitializeComponent();

    webBrowser1.IsWebBrowserContextMenuEnabled = false;
    webBrowser1.ContextMenuStrip = contextMenuStrip_Text;

    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

    webBrowser1.Navigate("http://www.google.com/");
}

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser1.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown);
}

void Document_MouseDown(object sender, HtmlElementEventArgs e)
{
    HtmlElement elem = webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition);

    for (int i = 0; i < webBrowser1.Document.Links.Count; i++)
    {
        if (elem == webBrowser1.Document.Links[i])
        {
            webBrowser1.ContextMenuStrip = contextMenuStrip_Links;
            return;
        }
    }

    webBrowser1.ContextMenuStrip = contextMenuStrip_Text;
}




/// METHOD 1 ///

C#
public partial class Form1 : Form
{
    HtmlElementCollection links = null;

    public Form1()
    {
        InitializeComponent();

        webBrowser1.Navigating += 
            new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);

        webBrowser1.DocumentCompleted +=
            new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

        webBrowser1.IsWebBrowserContextMenuEnabled = false;
        webBrowser1.ContextMenuStrip = contextMenuStrip2;// You're contextMenuStrip_Text

        webBrowser1.Navigate("http://www.google.com/");
    }

    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        links = webBrowser1.Document.Links;
        for (int i = 0; i < links.Count; i++)
        {
            links[i].MouseEnter += new HtmlElementEventHandler(link_MouseEnter);
            links[i].MouseLeave += new HtmlElementEventHandler(link_MouseLeave);
        }
    }

    void link_MouseEnter(object sender, HtmlElementEventArgs e)
    {
        // show custom context menu strip for links
        webBrowser1.ContextMenuStrip = contextMenuStrip1;// You're contextMenuStrip_Links
    }

    void link_MouseLeave(object sender, HtmlElementEventArgs e)
    {
        // show custom context menu strip for everything else
        webBrowser1.ContextMenuStrip = contextMenuStrip2;// You're contextMenuStrip_Text
    }

    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        if (links != null)
        {
            for (int i = 0; i < links.Count; i++)
            {
                links[i].MouseEnter -= link_MouseEnter;
                links[i].MouseLeave -= link_MouseLeave;
            }
        }
    }
}
 
Share this answer
 
v12
Comments
_Q12_ 7-Feb-12 14:22pm    
---50% Bingo---
Now it shows both menus
1.when over the link is show the customized one.
2.when normal, it show the default menu(IE). --- I want a custom #2 menu appear here.
Can you suggest one variant?
In principle you did the hard work I've asked so +5. :)
thanks.
LanFanNinja 7-Feb-12 14:24pm    
See let me get this straight you want one custom menu for links and another custom menu for everything else? Let me know and I will update my solution for you.
_Q12_ 7-Feb-12 14:26pm    
Yes.
LanFanNinja 7-Feb-12 14:31pm    
Recheck my solution. This code should do what you want.
_Q12_ 7-Feb-12 14:40pm    
YEEES. THANK YOU.
You are the king.
In 1000 years I not come with a beautiful solution than you provided here.
----
As a final request(optional), can you point me some nice and fun links to learn these damn events? I want to settle with them once and for all.
Thank you very much.
I think you'll find the web browser control is not going to send an event to your form when the icon changes. Also, if you don't really understand how events work, you should do some reading on that online. What's the purpose of a web browser control with your own right click menu ? I'd be surprised if that was possible, too, given that the browser has it's own right click menu.
 
Share this answer
 
Comments
_Q12_ 7-Feb-12 13:33pm    
hy Christian
I already specify the: [webBrowser1.IsWebBrowserContextMenuEnabled = false;] because I don't like its default menu. I think that if my mouse over a link is changing, then it must be a way to catch its changing from Form1.
It is that hard, to do this?
Then I will rethink my needs, recustomize the interface, etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900