Como vincular a um objeto ou página da Web com o controle LinkLabel dos Windows Forms

O controle Windows Forms LinkLabel permite que você crie links de estilo da Web em seu formulário. Ao clicar no link, é possível alterar sua cor para indicar que o link foi visitado. Para obter mais informações sobre como alterar a cor, consulte Como alterar a aparência do controle LinkLabel do Windows Forms.

Vinculando a outro formulário

  1. Defina a propriedade como uma legenda Text apropriada.

  2. Defina a LinkArea propriedade para determinar qual parte da legenda será indicada como um link. Como isso é indicado depende das propriedades relacionadas à aparência do rótulo do link. O LinkArea valor é representado por um LinkArea objeto que contém dois números, a posição do caractere inicial e o número de caracteres. A LinkArea propriedade pode ser definida na janela Propriedades ou no código de maneira semelhante à seguinte:

    ' 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 No manipulador de eventos, chame o Show método para abrir outro formulário no projeto e defina a LinkVisited propriedade como true.

    Observação

    Uma instância da classe carrega uma referência ao LinkLabel controle que foi clicado, portanto, não há necessidade de LinkLabelLinkClickedEventArgs converter o sender objeto.

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

Vinculando a uma página da Web

O LinkLabel controle também pode ser usado para exibir uma página da Web com o navegador padrão.

  1. Defina a propriedade como uma legenda Text apropriada.

  2. Defina a LinkArea propriedade para determinar qual parte da legenda será indicada como um link.

  3. LinkClicked No manipulador de eventos, no meio de um bloco de manipulação de exceções, chame um segundo procedimento que define a LinkVisited propriedade true e usa o método para iniciar o Start navegador padrão com uma URL. Para usar o método, Start você precisa adicionar uma referência ao System.Diagnostics namespace.

    Importante

    Se o código abaixo for executado em um ambiente de confiança parcial (como em uma unidade compartilhada), o compilador JIT falhará quando o método VisitLink for chamado. A System.Diagnostics.Process.Start instrução causa uma demanda de link que falha. Ao capturar a exceção quando o método VisitLink é chamado, o código a seguir garante que, se o compilador JIT falhar, o erro será tratado normalmente.

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

Confira também