mikelt21
mikelt21

Reputation: 2778

How to clear WebBrowser control in WPF

I'm using the code from the following link: Displaying html from string in WPF WebBrowser control

It works well except when I delete the item containing the html, e.NewValue becomes null and I get an exception. Is there a way to return the WebBrowser control to a blank screen?

Upvotes: 12

Views: 19845

Answers (2)

mikelt21
mikelt21

Reputation: 2778

I found this. Anyone have anything better?

if (wb != null)
{
    if (e.NewValue != null)
        wb.NavigateToString(e.NewValue as string);
    else
        wb.Navigate("about:blank");
}

EDIT:

As poby mentioned in the comments, for .NET 4+ use:

if (wb != null)
{
    if (e.NewValue != null)
        wb.NavigateToString(e.NewValue as string);
    else
        wb.Navigate((Uri)null);
}

Upvotes: 21

ulatekh
ulatekh

Reputation: 1500

I just set the WebBrowserLabel's DocumentText to string.Empty.

Upvotes: 0

Related Questions