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



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)

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

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

  1. Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. In the Name box, type LinkLabel, and then click OK.

    By default, the Form1 form is created and opened in Design mode.
  5. Add a LinkLabel control to the Form1 form.
  6. Right-click linkLabel1, and then click Properties.
  7. Change the Name property of linkLabel1 to libLink.
  8. Double-click Form1 to add the Form1_Load event handler in the code window.
  9. 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

  1. Add the following code to the top of the code window after the other using directives:
    using namespace System::Diagnostics;
  2. On the View menu, click Designer to switch to Design mode.
  3. Double-click linkLabel1 to add a libLink_LinkClicked event handler in the code window.
  4. 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:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

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

REFERENCES

For additional information about System.Diagnostics Hierarchy, visit the following Microsoft Developer Network (MSDN) Web site:Back to the top

Modification Type:MajorLast Reviewed:1/17/2006
Keywords:kbWindowsForms kbshell kbForms kbDiagnostics kbControl kbCollections kbProperties kbHOWTOmaster KB816187 kbAudDeveloper