Procedura: eseguire il collegamento a un oggetto o a una pagina Web con il controllo LinkLabel di Windows Form

Il controllo LinkLabel di Windows Form consente di creare collegamenti ipertestuali all'interno del form. Se si fa clic sul collegamento, è possibile modificarne il colore per indicare che tale collegamento è stato visitato. Per ulteriori informazioni sulla modifica del colore, vedere Procedura: modificare l'aspetto del controllo LinkLabel di Windows Form.

Collegamento a un altro form

Per creare un collegamento a un altro form tramite un controllo LinkLabel

  1. Impostare la proprietà Text su un tipo di didascalia appropriato.

  2. Impostare la proprietà LinkArea per stabilire quale parte della didascalia verrà indicata come collegamento. Il modo in cui il collegamento viene segnalato dipende dalle proprietà di aspetto del collegamento ipertestuale. Il valore LinkArea viene rappresentato da un oggetto LinkArea contenente due numeri, la posizione del carattere iniziale e il numero di caratteri. La proprietà LinkArea può essere impostata nella finestra Proprietà oppure nel codice in modo analogo all'esempio riportato di seguito:

    ' 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. Nel gestore eventi LinkClicked richiamare il metodo Show per aprire un altro form nel progetto e impostare la proprietà LinkVisited su true.

    Nota

    Un'istanza della classe LinkLabelLinkClickedEventArgs contiene un riferimento al controllo LinkLabel scelto, pertanto non è necessario eseguire il cast dell'oggetto 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;
       }
    

Collegamento a una pagina Web

Il controllo LinkLabel può essere utilizzato anche per visualizzare una pagina Web con il browser predefinito.

Per avviare Internet Explorer e creare un collegamento a una pagina Web tramite un controllo LinkLabel

  1. Impostare la proprietà Text su un tipo di didascalia appropriato.

  2. Impostare la proprietà LinkArea per stabilire quale parte della didascalia verrà indicata come collegamento.

  3. Nel gestore eventi LinkClicked, nella parte centrale di un blocco di gestione delle eccezioni, chiamare una seconda procedura che imposti la proprietà LinkVisited su true e utilizzi il metodo Start per avviare il browser predefinito con l'URL desiderato. Per utilizzare il metodo Start, è necessario aggiungere un riferimento allo spazio dei nomi System.Diagnostics.

    Nota sulla sicurezzaNota sulla sicurezza

    Se il codice riportato di seguito viene eseguito in un ambiente parzialmente attendibile, ad esempio su un'unità condivisa, il compilatore JIT restituirà un errore quando viene chiamato il metodo VisitLink. L'istruzione System.Diagnostics.Process.Start implica infatti una richiesta di collegamento in errore. Intercettando l'eccezione durante la chiamata del metodo VisitLink, il codice riportato di seguito garantisce una corretta gestione dell'eventuale errore generato del compilatore 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("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");
       }
    

Vedere anche

Attività

Procedura: modificare l'aspetto del controllo LinkLabel di Windows Form

Riferimenti

Process.Start

Cenni preliminari sul controllo LinkLabel (Windows Form)

Altre risorse

Controllo LinkLabel (Windows Form)