如何:使用 Windows 窗体 LinkLabel 控件链接到对象或网页

Windows 窗体 LinkLabel 控件允许你在窗体上创建 Web 样式链接。 单击链接后,可以更改其颜色以指示已访问链接。 有关更改颜色的详细信息,请参阅 “如何:更改 Windows 窗体 LinkLabel 控件的外观”。

链接到另一个表单

  1. Text 属性设置为适当的题注。

  2. 设置属性 LinkArea 以确定标题的哪个部分将指示为链接。 指示方式取决于链接标签的外观相关属性。 该值 LinkArea 由包含两个 LinkArea 数字、起始字符位置和字符数的对象表示。 可以在“属性”窗口中或代码中设置该 LinkArea 属性,其方式类似于以下内容:

    ' 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);
    
  3. 在事件处理程序中 LinkClicked ,调用 Show 方法以打开项目中的另一个窗体,并将属性设置为 LinkVisitedtrue

    注释

    LinkLabelLinkClickedEventArgs 类的一个实例包含对被点击的 LinkLabel 控件的引用,因此无需对 sender 对象进行强制类型转换。

    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;
       }
    

链接到网页

LinkLabel 控件还可用于使用默认浏览器显示网页。

  1. Text 属性设置为适当的题注。

  2. 设置属性 LinkArea 以确定标题的哪个部分将指示为链接。

  3. LinkClicked 事件处理程序中,在异常处理块中,调用第二个过程,将 LinkVisited 属性 true 设置为并使用 Start 该方法以 URL 启动默认浏览器。 若要使用 Start 该方法,需要添加对命名空间的 System.Diagnostics 引用。

    重要

    如果下面的代码在部分信任环境中(例如在共享驱动器上运行),则调用该方法时 VisitLink JIT 编译器会失败。 该 System.Diagnostics.Process.Start 语句导致链接需求失败。 通过捕获调用方法时的 VisitLink 异常,下面的代码可确保如果 JIT 编译器失败,则会正常处理错误。

    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
    
    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");
    }
    
    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");
       }
    

另请参阅