Windows Form LinkLabel 控制項可讓您在表單上建立 Web 樣式連結。 按一下連結時,您可以變更其色彩以指示已瀏覽該連結。 如需變更色彩的詳細資訊,請參閱 HOW TO:變更 Windows Form LinkLabel 控制項的外觀。
連結至另一個表單
若要使用 LinkLabel 控制項連結至另一個表單
將 Text 屬性設定為適當的標題。
設定 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);在 LinkClicked 事件處理常式中叫用 Show 方法在專案中開啟另一個表單,並將 LinkVisited 屬性設定為 true。
注意事項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 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; }
連結至 Web 網頁
LinkLabel 控制項也可使用預設的瀏覽器來顯示 Web 網頁。
若要啟動 Internet Explorer 及使用 LinkLabel 控制項連結至 Web 網頁
將 Text 屬性設定為適當的標題。
設定 LinkArea 屬性,決定標題的哪個部分會表示為連結。
在 LinkClicked 事件處理常式的例外處理區塊當中,呼叫將 LinkVisited 屬性設定為 true,並使用 Start 方法的第二個程序,以 URL 啟動預設瀏覽器。 若要使用 Start 方法,您需要加入 System.Diagnostics 命名空間的參考。
安全性注意事項如果以下程式碼是在部分信任的環境 (例如共用磁碟機) 中執行,則在呼叫 VisitLink 方法時,JIT 編譯器會失敗。 System.Diagnostics.Process.Start 陳述式造成失敗的連結要求。 以下程式碼藉由在呼叫 VisitLink 方法時攔截例外狀況 (Exception),可確保萬一 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 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("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"); }
請參閱
工作
HOW TO:變更 Windows Form LinkLabel 控制項的外觀
參考
LinkLabel 控制項概觀 (Windows Form)