PROWAREtech

articles » archived » dot-net » example-webbrowser-control

.NET: WebBrowser Control Example

An example using the WebBrowser control written in C# built with .NET Framework.

This .NET application is a basic example of using the WebBrowser control which allows embedding a web browser in an application. This could make implementing online help from within an app very easy. Its rendering is more basic than that of a formal browser so it cannot really be used to create an actual web browser.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ExampleProgram
{
	class WebBrowserEx : Form
	{
		private WebBrowser wb;
		private TextBox tb;
		[STAThread]
		static void Main()
		{
			Application.Run(new WebBrowserEx());
		}
		public WebBrowserEx()
		{
			Text = "WebBrowser Example";
			BackColor = SystemColors.Window;
			ForeColor = SystemColors.WindowText;

			tb = new TextBox();
			tb.Parent = this;
			tb.BackColor = SystemColors.Window;
			tb.ForeColor = SystemColors.WindowText;
			tb.Top = 0;
			tb.Left = 0;
			tb.Width = ClientSize.Width;
			tb.Height = 16;
			tb.BorderStyle = BorderStyle.FixedSingle;
			tb.KeyPress += tb_KeyPress;

			wb = new WebBrowser();
			wb.Parent = this;
			wb.Top = 17;
			wb.Left = 0;
			wb.Width = ClientSize.Width;
			wb.Height = ClientSize.Height - 17;

			ClientSizeChanged += Form_ClientSizeChanged;
		}
		private void Form_ClientSizeChanged(object sender, EventArgs e)
		{
			tb.Width = ClientSize.Width;
			wb.Width = ClientSize.Width;
			wb.Height = ClientSize.Height - 17;
		}
		private void tb_KeyPress(object sender, KeyPressEventArgs e)
		{
			if(e.KeyChar == (char)13)
			{
				if(!string.IsNullOrWhiteSpace(tb.Text))
					wb.Navigate(tb.Text);
			}
		}
	}
}

PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
ACCEPT REJECT