* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
7.7 KiB
title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | description | ms.date | dev_langs | helpviewer_keywords | ms.assetid | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Link to an Object or Web Page with LinkLabel Control | Learn how to create Web-style links to an object or web page with the Windows Forms LinkLabel control. | 03/30/2017 |
|
|
6c91c975-3cb7-4504-82f0-fc6255f8fb85 |
How to: Link to an Object or Web Page with the Windows Forms LinkLabel Control
The Windows Forms xref:System.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 xref:System.Windows.Forms.LinkLabel.Text%2A property to an appropriate caption.
-
Set the xref:System.Windows.Forms.LinkLabel.LinkArea%2A 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 xref:System.Windows.Forms.LinkLabel.LinkArea%2A value is represented by a xref:System.Windows.Forms.LinkLabel.LinkArea%2A object containing two numbers, the starting character position and the number of characters. The xref:System.Windows.Forms.LinkLabel.LinkArea%2A 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 xref:System.Windows.Forms.LinkLabel.LinkClicked event handler, invoke the xref:System.Windows.Forms.Form.Show%2A method to open another form in the project, and set the xref:System.Windows.Forms.LinkLabel.LinkVisited%2A property to
true.Note
An instance of the xref:System.Windows.Forms.LinkLabelLinkClickedEventArgs class carries a reference to the xref:System.Windows.Forms.LinkLabel control that was clicked, so there is no need to cast the
senderobject.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 Subprotected 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 xref:System.Windows.Forms.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 xref:System.Windows.Forms.LinkLabel.Text%2A property to an appropriate caption.
-
Set the xref:System.Windows.Forms.LinkLabel.LinkArea%2A property to determine which part of the caption will be indicated as a link.
-
In the xref:System.Windows.Forms.LinkLabel.LinkClicked event handler, in the midst of an exception-handling block, call a second procedure that sets the xref:System.Windows.Forms.LinkLabel.LinkVisited%2A property to
trueand uses the xref:System.Diagnostics.Process.Start%2A method to start the default browser with a URL. To use the xref:System.Diagnostics.Process.Start%2A method you need to add a reference to the xref:System.Diagnostics?displayProperty=nameWithType 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
VisitLinkmethod is called. TheSystem.Diagnostics.Process.Startstatement causes a link demand that fails. By catching the exception when theVisitLinkmethod 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("http://www.microsoft.com") End Subprivate 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"); }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"); }