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 topRequirements
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 topAdd 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.
- Change the Name property of linkLabel1 to
libLink.
- Double-click Form1 to add the
Form1_Load event handler in the code window.
- 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"http://msdn.microsoft.com/library/");
Back to the top Start the default Web
browser
- Add the following code to the top of the code window after
the other using directives:
using namespace System::Diagnostics;
- On the View menu, click
Designer to switch to Design mode.
- Double-click linkLabel1 to add a
libLink_LinkClicked event handler in the code
window.
- 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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- 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.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site:
Back to the topVerify that it
works
- Press CTRL+SHIFT+B to build the
solution.
- Press CTRL+F5 to run the
project.
- Click linkLabel1.
Notice that the default browserit
starts, and opens the URL that is associated with the
LinkLabel control.
Back to the
top