Microsoft KB Archive/816187

= How to start the default Web browser by using a LinkLabel control in Windows Forms with Visual C++ .NET or with Visual C++ 2005 =

Article ID: 816187

Article Last Modified on 1/11/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-







For a Microsoft Visual Basic .NET version of this article, see 320320.



For a Microsoft Visual C# .NET version of this article, see 320478.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System::ComponentModel
 * System::Collections
 * System::Windows::Forms
 * System::Data
 * System::Drawing
 * System::Diagnostics

IN THIS TASK

 * INTRODUCTION
 * Requirements
 * Add a LinkLabel control and a link to your form
 * Start the default Web browser
 * Verify that It works
 * REFERENCES



INTRODUCTION
This step-by-step article describes how to use a LinkLabel control in the Microsoft .NET Framework System::Windows::Forms namespace 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, it does not contain a simple method to open the URL in an external browser when you click the label. The Process class in the .NET Framework System::Diagnostics namespace makes this method easy. The Start method of Process class takes an initial argument of the ProcessStartInfo object. The ProcessStartInfo object, in turn, takes a path of a file. This path is either a local file path or a URL. The ProcessStartInfo class uses the content type to open the file in an appropriate program. This emulates the Win32 ShellExecute function. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

174156 HOWTO: Programmatically Launch the Default Internet Browser

Back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005
 * Microsoft .NET Framework 1.1 or later

This article assumes that you are familiar with the following topics:
 * Microsoft Visual C++ .NET 2003 or Microsoft Visual C++ 2005

Back to the top

Add a LinkLabel control and a link to your form
 Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project. Under Project Types, click Visual C++ Projects, and then click Windows Forms Application (.NET) under Templates.

Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Forms Application under Templates. In the Name box, type LinkLabel, and then click OK.

By default, the Form1 form is created and opened in Design mode. Add a LinkLabel control to the Form1 form. Right-click linkLabel1, and then click Properties.</li> Change the Name property of linkLabel1 to libLink.</li> Double-click Form1 to add the Form1_Load event handler in the code window.</li>  Add the following code in the Form1_Load event handler: libLink->Links->Remove(libLink->Links->get_Item(0)); libLink->Links->Add(0, libLink->Text->Length, S&quot;http://msdn.microsoft.com/library/&quot;); </li></ol>

Back to the top

Start the default Web browser
<ol>  Add the following code to the top of the code window after the other using directives: using namespace System::Diagnostics; </li> On the View menu, click Designer to switch to Design mode.</li> Double-click linkLabel1 to add a libLink_LinkClicked event handler in the code window.</li>  Add the following code in the libLink_LinkClicked event handler: ProcessStartInfo * sInfo = new ProcessStartInfo(e->Link->LinkData->ToString); Process::Start(sInfo); Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li></ol>

Back to the top

Verify that it works

 * 1) Press CTRL+SHIFT+B to build the solution.
 * 2) Press CTRL+F5 to run the project.
 * 3) Click linkLabel1.

Notice that the default browserit starts, and opens the URL that is associated with the LinkLabel control.

Back to the top

<div class="references_section">