--- title: Link to an Object or Web Page with LinkLabel Control description: Learn how to create Web-style links to an object or web page with the Windows Forms LinkLabel control. ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" - "cpp" helpviewer_keywords: - "examples [Windows Forms], LinkLabel control" - "Windows Forms, linking to objects" - "Web page link control" - "linking [Windows Forms], to other forms" - "Windows Forms, linking to Web pages" - "links [Windows Forms], to other forms" - "LinkLabel control [Windows Forms], linking to object or Web page" - "LinkLabel control [Windows Forms], examples" ms.assetid: 6c91c975-3cb7-4504-82f0-fc6255f8fb85 --- # How to: Link to an Object or Web Page with the Windows Forms LinkLabel Control The Windows Forms control allows you to create Web-style links on your form. When the link is clicked, you can change its color to indicate the link has been visited. For more information on changing the color, see [How to: Change the Appearance of the Windows Forms LinkLabel Control](how-to-change-the-appearance-of-the-windows-forms-linklabel-control.md). ## Linking to Another Form #### To link to another form with a LinkLabel control 1. Set the property to an appropriate caption. 2. Set the property to determine which part of the caption will be indicated as a link. How it is indicated depends on the appearance-related properties of the link label. The value is represented by a object containing two numbers, the starting character position and the number of characters. The property can be set in the Properties window or in code in a manner similar to the following: ```vb ' In this code example, the link area has been set to begin ' at the first character and extend for eight characters. ' You may need to modify this based on the text entered in Step 1. LinkLabel1.LinkArea = New LinkArea(0, 8) ``` ```csharp // In this code example, the link area has been set to begin // at the first character and extend for eight characters. // You may need to modify this based on the text entered in Step 1. linkLabel1.LinkArea = new LinkArea(0,8); ``` ```cpp // In this code example, the link area has been set to begin // at the first character and extend for eight characters. // You may need to modify this based on the text entered in Step 1. linkLabel1->LinkArea = LinkArea(0,8); ``` 3. In the event handler, invoke the method to open another form in the project, and set the property to `true`. > [!NOTE] > An instance of the class carries a reference to the control that was clicked, so there is no need to cast the `sender` object. ```vb Protected Sub LinkLabel1_LinkClicked(ByVal Sender As System.Object, _ ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles LinkLabel1.LinkClicked ' Show another form. Dim f2 As New Form() f2.Show LinkLabel1.LinkVisited = True End Sub ``` ```csharp protected void linkLabel1_LinkClicked(object sender, System. Windows.Forms.LinkLabelLinkClickedEventArgs e) { // Show another form. Form f2 = new Form(); f2.Show(); linkLabel1.LinkVisited = true; } ``` ```cpp private: void linkLabel1_LinkClicked(System::Object ^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs ^ e) { // Show another form. Form ^ f2 = new Form(); f2->Show(); linkLabel1->LinkVisited = true; } ``` ## Linking to a Web Page The control can also be used to display a Web page with the default browser. #### To start Internet Explorer and link to a Web page with a LinkLabel control 1. Set the property to an appropriate caption. 2. Set the property to determine which part of the caption will be indicated as a link. 3. In the event handler, in the midst of an exception-handling block, call a second procedure that sets the property to `true` and uses the method to start the default browser with a URL. To use the method you need to add a reference to the namespace. > [!IMPORTANT] > If the code below is run in a partial-trust environment (such as on a shared drive), the JIT compiler fails when the `VisitLink` method is called. The `System.Diagnostics.Process.Start` statement causes a link demand that fails. By catching the exception when the `VisitLink` method is called, the code below ensures that if the JIT compiler fails, the error is handled gracefully. ```vb Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _ Handles LinkLabel1.LinkClicked Try VisitLink() Catch ex As Exception ' The error message MessageBox.Show("Unable to open link that was clicked.") End Try End Sub Sub VisitLink() ' Change the color of the link text by setting LinkVisited ' to True. LinkLabel1.LinkVisited = True ' Call the Process.Start method to open the default browser ' with a URL: System.Diagnostics.Process.Start("http://www.microsoft.com") End Sub ``` ```csharp private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e) { try { VisitLink(); } catch (Exception ex ) { MessageBox.Show("Unable to open link that was clicked."); } } private void VisitLink() { // Change the color of the link text by setting LinkVisited // to true. linkLabel1.LinkVisited = true; //Call the Process.Start method to open the default browser //with a URL: System.Diagnostics.Process.Start("http://www.microsoft.com"); } ``` ```cpp private: void linkLabel1_LinkClicked(System::Object ^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs ^ e) { try { VisitLink(); } catch (Exception ^ ex) { MessageBox::Show("Unable to open link that was clicked."); } } private: void VisitLink() { // Change the color of the link text by setting LinkVisited // to true. linkLabel1->LinkVisited = true; // Call the Process.Start method to open the default browser // with a URL: System::Diagnostics::Process::Start("http://www.microsoft.com"); } ``` ## See also - - [LinkLabel Control Overview](linklabel-control-overview-windows-forms.md) - [How to: Change the Appearance of the Windows Forms LinkLabel Control](how-to-change-the-appearance-of-the-windows-forms-linklabel-control.md) - [LinkLabel Control](linklabel-control-windows-forms.md)