views:

1176

answers:

3

We are using the WebBrowser control in c# winforms and need to be able to get information about the Url the cursor is positions on.

So we have a web page in design mode, which has multiple urls, when the cursor is over one i would like to call a method which would return the id of the link.

Thanks

A: 

The webBrowser control has a Document property which has a Links collection. Each Link is an HTMLElement which has events you can tap into. Again, I'm not sure what you mean "cursor" because in the web world, unless if you're in a textbox, there really isn't a "cursor" (which is what I meant to ask in my comment) but you can tap into the MouseOver event and other stuff like that.

Example:

    foreach (HtmlElement element in this.webBrowser1.Document.Links)
    {
        element.MouseOver += (o, ex) =>
        {
            Console.WriteLine(ex.ToElement.GetAttribute("HREF"));
        };
    }

This will print out the actual URL that the mouse is over.

BFree
A: 

You can have a look at this article - Hosting a web browser component in a C# winform - which explains several ways to perform that. or go directly to this one - Hosting a webpage inside a Windows Form - Basically what you need to do is handle the Click of the DOM object inside the COM WebBrowser of IE. You achieve this by handling the Js events inside your C# code.

I remember this kind of customization must be done using the AxSHDocVw.AxWebBrowser COM object instead of the System.Windows.Forms.WebBrowser Class from the newer versions of the .Net Framework.

I could send you more data about this, I did it some project, just give me time to find it ;). In the mean time try with those links.

By!

BrutusCat
+1  A: 

You can use the IHTMLCaret to get the cursor position from there using IMarkupPointer you can get the element in the current scope

Kev Hunter