views:

60

answers:

2

I'm having issues trying to get the document title from a WebBrowser in C#. It works fine in VB.NET, but it won't give me any properties in C#.

When I type in MyBrowser.Document., the only options I get are 4 methods: Equals, GetHashCode, GetType, and ToString - no properties.

I think it's because I have to assign the document to a new instance first, but I can't find the HTMLDocument class that exists in VB.NET.

Basically what I'm wanting to do is return the Document.Title each time the WebBrowser loads/reloads a page.

Can someone help please? It will be much appreciated!

Here is the code I have at the moment...

private void Link_Click(object sender, RoutedEventArgs e)
{
    WebBrowser tempBrowser = new WebBrowser();
    tempBrowser.HorizontalAlignment = HorizontalAlignment.Left;
    tempBrowser.Margin = new Thickness(-4, -4, -4, -4);
    tempBrowser.Name = "MyBrowser";
    tempBrowser.VerticalAlignment = VerticalAlignment.Top;
    tempBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(tempBrowser_LoadCompleted);

    tempTab.Content = tempBrowser; // this is just a TabControl that contains the WebBrowser

    Uri tempURI = new Uri("http://www.google.com");
    tempBrowser.Navigate(tempURI);
}

private void tempBrowser_LoadCompleted(object sender, EventArgs e)
{
    if (sender is WebBrowser)
    {
        MessageBox.Show("Test");
        currentBrowser = (WebBrowser)sender;
        System.Windows.Forms.HtmlDocument tempDoc = (System.Windows.Forms.HtmlDocument)currentBrowser.Document;
        MessageBox.Show(tempDoc.Title);
    }
}

This code doesn't give me any errors, but I never see the second MessageBox. I do see the first one though (the "Test" message), so the program is getting to that code block.

A: 
string title = ((HTMLDocument)MyBrowser.Document).Title

Or

HTMLDocument Doc =  (HTMLDocument)MyBrowser.Document.Title ;
string title = doc.Title;
Conrad Frix
+1  A: 

You are not using the Windows Forms WebBrowser control. I think you got the COM wrapper for ieframe.dll, its name is AxWebBrowser. Verify that by opening the References node in the Solution Explorer window. If you see AxSHDocVw then you got the wrong control. It is pretty unfriendly, it just gives you an opaque interface pointer for the Document property. You'll indeed only get the default object class members.

Look in the toolbox. Pick WebBrowser instead of "Microsoft Web Browser".

Hans Passant
Hans, the Solution Explorer shows SHDocVw. My app is in WPF, and I just used the default WebBrowser control in it. Do I need to add the "using System.Windows.Forms" reference and use that WebBrowser instead?
Randy
Well, that's it then. Check this: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/e60b671e-84e6-40a4-a37a-e0a8610aef44
Hans Passant
Thanks, I will look into the link and see what I can do. What I find strange, however, is that I've worked a bit more on my current code, and I'm not getting any errors, but I'm not getting what I have it trying to do either. Can you look at the code in my edited post above and see if anything looks off with it? It may be that it just isn't possible, but I would think it would give me an error message if that was the case.
Randy
That code cannot work, not sure why your program doesn't bomb from the InvalidCast exception.
Hans Passant
Okay, I used the WindowsFormsHost as you directed, and it's working like a charm! Thank you very much!
Randy