Creating a Windows Forms Application C# Help

To see ActiveX controls running inside a Windows Forms application, create a simple Windows Forms
application project.

With this application, you will build a simple Internet browser that uses the Web Browser control which comes as part of the operating system.

The form should include a toolstrip with a text box and three
buttons.

The text box with the name toolStripTextUrl is used to enter a URL, three buttons with the names toolStripButtonNavigate, toolStripButtonBack,and toolStripButtonForward to
navigate web pages, and a status strip with the name statusStrip.

The status strip also needs a label to display status messages.

Figure 24-12

Figure 24-12

Using Visual Studio, you can add ActiveX controls to the toolbar to use it in the same way as a Windows Forms control.

On the Customize Toolbox context menu, select the Add/Remove Items menu entry and select the Microsoft WebBrowser control in the COM Components category.(see Figure 24-13).

Figure 24-13

This way, an icon will show up in the toolbox.

Similarly to other Windows controls, you can drag and drop this icon to the Windows Forms designer to create (with the aximp utility) a wrapper assembly hosting the ActiveX control.

You can see the wrapper assemblies with the references in the project:AxSHDocVwand SHDocVwN.Now you can invoke methods of the control by using the generated variable axwebBrowser1, as shown in the following code. Add a Click event handler to the button
toolStripButtonNavigate in order to navigate the browser to a web page. The method Navigate () used for this purpose requires a URL string with the first argument that you get by accessing the Text property of the text box control toolStripTextUrl:

private void OnNavigate(object sender. System.EventArgs e)
{
try
(
axWebBrowserl.Navigate(toolStripTextUrl.Text);
}
catch (COMException ex)
(
statusStrip.Items[Ol.Text ex.Message;

With the Click event handler of the Back and Forward buttons, call the GoBack () and GoForward ( ) methods of the browser control:

private void OnGoBack(object sender. System.EventArgs e)
{
try
{
axWebBrowserl.GoBack() ;
}
catch (COMExceptionex)

statusSt~ip_Items[OJ .Text ex.Message;
private void OnGoForward(object sender, System.EventArgs e)
{
try
(
axWebBrowserl.GoForward(),
}
catch (COMException ex)
(
statusStrip.ltems[O].Text ex.Message;

The web control also offers some events that can be used just like a .NET event.Add the event handler OnStatusChange () to the event StatusTextChange to setthe status that is returned by the control to the status stripin the Windows Forms application:

The web control also offers some events that can be used just like a .NET event.Add the event handler On Status Change () to the event Status Text Change to set the status that is returned by the control to
the status stripin the Windows Forms application:

private void OnStatusChange(object sender,
AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEvent e)
statu~Strip.ltems[OJ .Text = e.text;

Now, you  have a simple browser that you can use to navigate to web pages.

Figure 24-14

Figure 24-14

Posted on October 27, 2015 in Interoperability

Share the Story

Back to Top