winforms - How to load local HTML pages in WebBrowser control in C#

Winforms - How to load local HTML pages in WebBrowser control in C#

To load local HTML pages in a WebBrowser control in a WinForms application using C#, you can set the Url or DocumentText property of the WebBrowser control. Below, We'll guide you through the steps to achieve this.

Step-by-Step Guide

  1. Add a WebBrowser Control to Your Form: Drag and drop a WebBrowser control onto your form from the Toolbox in Visual Studio.

  2. Load Local HTML File: Use the WebBrowser control's Navigate method to load a local HTML file.

Example Code

Here is an example of how to load a local HTML file into the WebBrowser control:

Method 1: Using the Navigate Method

This method is useful if you have the HTML file saved on your local disk.

using System; using System.Windows.Forms; namespace YourNamespace { public partial class YourForm : Form { public YourForm() { InitializeComponent(); // Load local HTML file when the form loads this.Load += new EventHandler(YourForm_Load); } private void YourForm_Load(object sender, EventArgs e) { // Set the path to your local HTML file string filePath = @"C:\path\to\your\file.html"; // Ensure the path is in a format that the WebBrowser control can understand Uri fileUri = new Uri(filePath); // Load the local HTML file webBrowser1.Navigate(fileUri); } } } 

Method 2: Using the DocumentText Property

This method is useful if you have the HTML content as a string in your application.

using System; using System.Windows.Forms; namespace YourNamespace { public partial class YourForm : Form { public YourForm() { InitializeComponent(); // Load HTML content when the form loads this.Load += new EventHandler(YourForm_Load); } private void YourForm_Load(object sender, EventArgs e) { // HTML content as a string string htmlContent = @" <!DOCTYPE html> <html> <head> <title>Local HTML</title> </head> <body> <h1>Hello, World!</h1> <p>This is a local HTML page.</p> </body> </html>"; // Load the HTML content webBrowser1.DocumentText = htmlContent; } } } 

Explanation

  1. Adding the WebBrowser Control: Drag and drop the WebBrowser control from the Toolbox onto your form in the designer.

  2. Loading Local HTML File:

    • Using Navigate Method:
      • Convert the file path to a URI using new Uri(filePath).
      • Use webBrowser1.Navigate(fileUri) to load the HTML file.
    • Using DocumentText Property:
      • Assign the HTML content directly to webBrowser1.DocumentText.
  3. Handling Form Load Event:

    • In the form's constructor, attach the Load event to a handler method.
    • In the handler method (YourForm_Load), load the HTML file or content into the WebBrowser control.

By following these steps, you can easily load local HTML pages into the WebBrowser control in a WinForms application, whether the HTML content is stored in a file or as a string within your application.

Examples

  1. How to load a local HTML file in WebBrowser control in C# WinForms?

    • Description: This example demonstrates how to load a local HTML file into the WebBrowser control.
    • Code:
      private void Form1_Load(object sender, EventArgs e) { string filePath = @"C:\path\to\your\file.html"; webBrowser1.Url = new Uri(filePath); } 
  2. How to display local HTML content in WebBrowser control using C# WinForms?

    • Description: This example shows how to display local HTML content in the WebBrowser control by setting the DocumentText property.
    • Code:
      private void Form1_Load(object sender, EventArgs e) { string htmlContent = System.IO.File.ReadAllText(@"C:\path\to\your\file.html"); webBrowser1.DocumentText = htmlContent; } 
  3. How to use WebBrowser control to navigate to a local HTML file in C#?

    • Description: This example demonstrates how to navigate the WebBrowser control to a local HTML file.
    • Code:
      private void Form1_Load(object sender, EventArgs e) { string filePath = @"C:\path\to\your\file.html"; webBrowser1.Navigate(new Uri(filePath)); } 
  4. How to embed a local HTML page in WebBrowser control in WinForms?

    • Description: This example shows how to embed a local HTML page into the WebBrowser control by setting the URL.
    • Code:
      private void Form1_Load(object sender, EventArgs e) { string filePath = @"C:\path\to\your\file.html"; webBrowser1.Url = new Uri(filePath); } 
  5. How to load HTML string into WebBrowser control in C# WinForms?

    • Description: This example demonstrates how to load an HTML string directly into the WebBrowser control.
    • Code:
      private void Form1_Load(object sender, EventArgs e) { string htmlContent = "<html><body><h1>Hello, World!</h1></body></html>"; webBrowser1.DocumentText = htmlContent; } 
  6. How to show local HTML file in WebBrowser control on button click in WinForms?

    • Description: This example demonstrates how to load a local HTML file into the WebBrowser control when a button is clicked.
    • Code:
      private void button1_Click(object sender, EventArgs e) { string filePath = @"C:\path\to\your\file.html"; webBrowser1.Navigate(new Uri(filePath)); } 
  7. How to dynamically load local HTML file in WebBrowser control in C#?

    • Description: This example shows how to dynamically load a local HTML file into the WebBrowser control based on user input.
    • Code:
      private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "HTML files (*.html)|*.html|All files (*.*)|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { webBrowser1.Navigate(new Uri(openFileDialog.FileName)); } } 
  8. How to render local HTML content in WebBrowser control in WinForms?

    • Description: This example demonstrates how to render local HTML content in the WebBrowser control by setting the DocumentStream property.
    • Code:
      private void Form1_Load(object sender, EventArgs e) { string filePath = @"C:\path\to\your\file.html"; using (StreamReader reader = new StreamReader(filePath)) { webBrowser1.DocumentStream = reader.BaseStream; } } 
  9. How to load and display HTML from local file in WebBrowser control using C#?

    • Description: This example shows how to load and display HTML from a local file into the WebBrowser control.
    • Code:
      private void Form1_Load(object sender, EventArgs e) { string filePath = @"C:\path\to\your\file.html"; webBrowser1.Navigate(new Uri(filePath)); } 
  10. How to show local HTML file content in WebBrowser control on form load in C#?

    • Description: This example demonstrates how to show local HTML file content in the WebBrowser control when the form loads.
    • Code:
      private void Form1_Load(object sender, EventArgs e) { string filePath = @"C:\path\to\your\file.html"; webBrowser1.Navigate(new Uri(filePath)); } 

More Tags

android-listview email-client nem progressdialog tableau-api tasklist email-ext datatable.select ibeacon centos6.5

More Programming Questions

More Chemical reactions Calculators

More Cat Calculators

More Mortgage and Real Estate Calculators

More Livestock Calculators