Ahmad
Ahmad

Reputation: 9678

How to show an HTML string in WebBrowser control when the address is "about:blank"

I would like to write a specific string (e.g. a help instuction) in the webbrowser control when it navigates to "about:blank", I can write my string in the Form_Load using DocumentText and it automatically navigates to "about:blank"

webBrowser1.DocumentText = "introduction....";

But now if the user refreshes the webbrowser control it shows a blank page. I would like it again shows my string whenever the address is "about:blank". Where is the best place to put my string into the webbrowser control?

Upvotes: 1

Views: 196

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125312

A document Refresh simply reloads the current page, so the Navigating, Navigated, and DocumentCompleted events do not occur when you call the Refresh method.

Using Navigating or Navigated event you should check if the browser is navigating or navigated to about:blank then disable the ways that user can refresh page, including browser shortcuts, browser context menu or any other point like custom toolbar buttons and context menus you created or refresh.

For other urls, enable them again.

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
    var state = (e.Url.ToString().ToLower() == "about:blank");
    this.webBrowser1.WebBrowserShortcutsEnabled = !state;
    this.webBrowser1.IsWebBrowserContextMenuEnabled = !state;
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    var content = "Custom Content";
    if (e.Url.ToString().ToLower() == "about:blank" &&
        this.webBrowser1.DocumentText != content)
    {
        this.webBrowser1.DocumentText = content;
    }
}

Upvotes: 1

Related Questions