Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How do I get the document title from a WebBrowser element?

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.

like image 797
Randy Cleary Avatar asked Jul 13 '10 15:07

Randy Cleary


People also ask

Is C still used?

The C programming language has been alive and kicking since 1972, and it still reigns as one of the fundamental building blocks of our software-studded world.

What is C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C coding?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

Add reference to Microsoft.mshtml

Add event receiver for LoadCompleted

webbrowser.LoadCompleted += new LoadCompletedEventHandler(webbrowser_LoadCompleted);

Then you will have no problems with document not being loaded in order to read values back out

    void webbrowser_LoadCompleted(object sender, NavigationEventArgs e)
    {
        // Get the document title and display it
        if (webbrowser.Document != null)
        {
            mshtml.IHTMLDocument2 doc = webbrowser.Document as mshtml.IHTMLDocument2;
            Informative.Text = doc.title;
        }
    }
like image 53
sturmstrike Avatar answered Sep 27 '22 18:09

sturmstrike


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".

like image 24
Hans Passant Avatar answered Sep 27 '22 17:09

Hans Passant