[C# Helper]
Index Books FAQ Contact About Rod
[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[Beginning Software Engineering]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Blank the WebBrowser control in C#

[Blank the WebBrowser control in C#]

If a program displays a web page in a WebBrowser control and then that page become irrelevant to the program, you may want to blank the control. Doing that is as easy as making the control navigate to the special URL about:blank. When you click this example's Blank button, the following code blanks the WebBrowser control.

// Blank the WebBrowser. private void btnBlank_Click(object sender, EventArgs e) { wbrCSharpHelper.Navigate("about:blank"); }

That's easy enough but there is a catch. The WebBrowser control takes some time to get ready to work and to load a web page. If you try to make it do something before it is ready, it sometimes fails. It seems to do okay with simple navigation, but actions such as trying to set the control's Document contents may cause trouble.

To avoid those kinds of errors, you should wait until the control's has finished loading whatever document it is loading before you do anything else. This probably isn't necessary in this example, but the program does it for form's sake.

At design time, I disabled the Blank button. I also set the WebBrowser control's URL property to books.html so it loads the C# Helper books page when it starts. When that page is finished loading, the control's DocumentCompleted event fires and the following event handler executes.

// Enable the Blank button. private void wbrCSharpHelper_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { btnBlank.Enabled = true; }

This code simply enables the Blank button so you can blank the control.

Download the example to experiment with it and to see additional details.

© 2009-2022 Rocky Mountain Computer Consulting, Inc. All rights reserved.