Procedimiento para establecer vínculos con un objeto o página web mediante el control LinkLabel de formularios Windows Forms
El control LinkLabel de Windows Forms permite crear vínculos de estilo web en el formulario. Al hacer clic en el vínculo, se puede cambiar su color para indicar que ese vínculo se ha visitado. Para obtener más información sobre cómo cambiar el color, vea Procedimiento para cambiar la apariencia del control LinkLabel de Windows Forms.
Vinculación a otro formulario
Para vincular a otro formulario con un control LinkLabel
Establezca la propiedad Text en un título adecuado.
Establezca la propiedad LinkArea para determinar qué parte del título se mostrará como un vínculo. La forma en que se esto se indique dependerá de las propiedades relacionadas con la apariencia de la etiqueta del vínculo. El valor de LinkArea se representa mediante un objeto LinkArea que contiene dos números, la posición del carácter inicial y el número de caracteres. La propiedad LinkArea se puede establecer en la ventana Propiedades o en el código de una manera similar a la siguiente:
' 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);
En el controlador de eventos LinkClicked, invoque al método Show para abrir otro formulario en el proyecto y establezca la propiedad LinkVisited en
true
.Nota:
Una instancia de la clase LinkLabelLinkClickedEventArgs hace referencia al control LinkLabel en el que se ha hecho clic, por lo que no es necesario convertir el objeto
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; }
Vinculación a una página web
El control LinkLabel también se puede usar para mostrar una página web con el explorador predeterminado.
Para abrir Internet Explorer y vincular a una página web con un control LinkLabel
Establezca la propiedad Text en un título adecuado.
Establezca la propiedad LinkArea para determinar qué parte del título se mostrará como un vínculo.
En el controlador de eventos LinkClicked, en medio de un bloque de control de excepciones, llame a un segundo procedimiento que establece la propiedad LinkVisited en
true
y use el método Start para iniciar el explorador predeterminado con una dirección URL. Para usar el método Start, debe agregar una referencia al espacio de nombres System.Diagnostics.Importante
Si el código siguiente se ejecuta en un entorno de confianza parcial (por ejemplo, en una unidad compartida), se produce un error en el compilador JIT al llamar al método
VisitLink
. La instrucciónSystem.Diagnostics.Process.Start
genera una demanda de vínculo que provoca un error. Al detectar la excepción cuando se llama al métodoVisitLink
, el código siguiente garantiza que, si se produce un error en el compilador JIT, dicho error se controla correctamente.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"); }
Consulte también
.NET Desktop feedback