HOW TO: Start the Default Web Browser by Using a LinkLabel Control in Windows Forms with Visual Basic .NET

Article translations Article translations
Article ID: 320320 - View products that this article applies to.
This article was previously published under Q320320
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step article demonstrates how to use a LinkLabel control in the Microsoft .NET Framework System.Windows.Forms namespace of the to open a Web address (or URL) in the default Web browser.

Although the LinkLabel control contains built-in facilities to associate URLs with arbitrary regions of a link, LinkLabel does not contain a simple method to open the URL in an external browser when you click the label. Fortunately, the Process class in the .NET Framework System.Diagnostics namespace makes this method easy. Process takes an initial argument of ProcessStartInfo. ProcessStartInfo, in turn, takes a path to a file. This path is either a local file path or an URL. ProcessStartInfo uses the content type to open the file in an appropriate program. This emulates the Win32 ShellExecute function. For additional information about this function, click the article number below to view the article in the Microsoft Knowledge Base:
174156 HOWTO: Programmatically Launch the Default Internet Browser

Add a LinkLabel and a Link to Your Form

  1. Follow these steps to create a new Windows Application project in Visual Basic .NET:
    1. Start Microsoft Visual Studio .NET.
    2. On the File menu, click New.
    3. In the Project Types box, click Visual Basic Projects. In the Templates box, click Windows Application.
  2. From the toolbox, drag a LinkLabel control to the existing Windows Form. If the Toolbox window is not visible, press CTRL+ALT+X, or click Toolbox on the View menu in Microsoft Visual Studio .NET.
  3. Click the LinkLabel control. In the Properties window, change the name of the LinkLabel control to libLink. If the Properties window is not visible, right-click the LinkLabel control, and then click Properties.
  4. Double-click anywhere in the form to open the Code window for the form. This places the insertion point in the form's Load method. Add the following code to the Load method:
            libLink.Links.Remove(libLink.Links(0))
            libLink.Links.Add(0, libLink.Text.Length, "http://msdn.microsoft.com/library/")
    					

Start the Default Web Browser

  1. In the Code window for your form, add the following line to the top of your form above the form's class definition:
    Imports System.Diagnostics
    					
  2. In the form's Designer window, double-click the LinkLabel control to add a LinkClicked event handler. This places the insertion point in the empty body of the new handler. Add the following code:
            Dim sInfo As New ProcessStartInfo(e.Link.LinkData.ToString())
            Process.Start(sInfo)
    					

Verify That It Works

  1. Press the F5 key, or click Start on the Debug menu in Visual Studio .NET, to run the program.
  2. Click linklabel1. Notice that this starts the default browser and opens the URL that is associated with the LinkLabel control.

REFERENCES

For more information, see the following MSDN Web site:
System.Diagnostics Hierarchy
http://msdn.microsoft.com/en-us/library/aa326903.aspx

Properties

Article ID: 320320 - Last Review: July 30, 2003 - Revision: 2.1
APPLIES TO
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
Keywords: 
kbhowtomaster KB320320

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com