How to: Link to an Object or Web Page with the Windows Forms LinkLabel Control
The Windows Forms LinkLabel 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.
Linking to Another Form
To link to another form with a LinkLabel control
Set the Text property to an appropriate caption.
Set the LinkArea 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 LinkArea value is represented by a LinkArea object containing two numbers, the starting character position and the number of characters. The LinkArea property can be set in the Properties window or in code in a manner similar to the following:
' 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)
// 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);
// 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);
In the LinkClicked event handler, invoke the Show method to open another form in the project, and set the LinkVisited property to true.
Note An instance of the LinkLabelLinkClickedEventArgs class carries a reference to the LinkLabel control that was clicked, so there is no need to cast the
sender
object.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
protected void linkLabel1_LinkClicked(object sender, System. Windows.Forms.LinkLabelLinkClickedEventArgs e) { // Show another form. Form f2 = new Form(); f2.Show(); linkLabel1.LinkVisited = true; }
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 LinkLabel 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
Set the Text property to an appropriate caption.
Set the LinkArea property to determine which part of the caption will be indicated as a link.
In the LinkClicked event handler, in the midst of an exception-handling block, call a second procedure that sets the LinkVisited property to true and uses the Start method to start the default browser with a URL. To use the Start method you need to add a reference to the System.Diagnostics namespace.
Security Note 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. TheSystem.Diagnostics.Process.Start
statement causes a link demand that fails. By catching the exception when theVisitLink
method is called, the code below ensures that if the JIT compiler fails, the error is handled gracefully.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("https://www.microsoft.com") End Sub
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("https://www.microsoft.com"); }
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("https://www.microsoft.com"); }
See Also
Tasks
How to: Change the Appearance of the Windows Forms LinkLabel Control
Reference
Process.Start
LinkLabel Control Overview (Windows Forms)