您可以變更 LinkLabel 控制項所顯示的文字,以符合各種用途。 例如,常見的做法是向使用者指出文字可以點擊,方法是將文字設定為使用特定色彩和底線來顯示。 使用者點擊文字之後,色彩會變更為不同的色彩。 若要控制此行為,您可以設定五個不同的屬性:LinkBehavior、LinkArea、LinkColor、VisitedLinkColor 和 LinkVisited 屬性。
若要變更 LinkLabel 控制項的外觀
將 LinkColor 和 VisitedLinkColor 屬性設定為您想要的色彩。
這可以透過程式設計方式或在 [屬性] 視窗中的設計階段完成。
' You can set the color using decimal values for red, green, and blue LinkLabel1.LinkColor = Color.FromArgb(0, 0, 255) ' Or you can set the color using defined constants LinkLabel1.VisitedLinkColor = Color.Purple// You can set the color using decimal values for red, green, and blue linkLabel1.LinkColor = Color.FromArgb(0, 0, 255); // Or you can set the color using defined constants linkLabel1.VisitedLinkColor = Color.Purple;// You can set the color using decimal values for red, green, and blue linkLabel1->LinkColor = Color::FromArgb(0, 0, 255); // Or you can set the color using defined constants linkLabel1->VisitedLinkColor = Color::Purple;將 Text 屬性設定為適當的標題。
這可以透過程式設計方式或在 [屬性] 視窗中的設計階段完成。
LinkLabel1.Text = "Click here to see more."linkLabel1.Text = "Click here to see more.";linkLabel1->Text = "Click here to see more.";設定 LinkArea 屬性以判斷標題的哪個部分會以連結表示。
LinkArea 值以包含兩個數字、起始字元位置和字元數的 LinkArea 來表示。 這可以透過程式設計方式或在 [屬性] 視窗中的設計階段完成。
LinkLabel1.LinkArea = new LinkArea(6,4)linkLabel1.LinkArea = new LinkArea(6,4);linkLabel1->LinkArea = LinkArea(6,4);將 LinkBehavior 屬性設定為 AlwaysUnderline、HoverUnderline 或 NeverUnderline。
如果設定為 HoverUnderline,則所 LinkArea 決定標題的部分只會在指標停留在標題上時加上底線。
在 LinkClicked 事件處理常式中,將 LinkVisited 屬性設定為
true。造訪連結時,常見的做法是以某種方式變更其外觀,通常是依色彩。 文字會變更為 VisitedLinkColor 屬性所指定的色彩。
Protected Sub LinkLabel1_LinkClicked (ByVal sender As Object, _ ByVal e As EventArgs) Handles LinkLabel1.LinkClicked ' Change the color of the link text ' by setting LinkVisited to True. LinkLabel1.LinkVisited = True ' Then do whatever other action is appropriate End Subprotected void LinkLabel1_LinkClicked(object sender, System.EventArgs e) { // Change the color of the link text by setting LinkVisited // to True. linkLabel1.LinkVisited = true; // Then do whatever other action is appropriate }private: System::Void linkLabel1_LinkClicked(System::Object ^ sender, System::Windows::Forms::LinkLabelLinkClickedEventArgs ^ e) { // Change the color of the link text by setting LinkVisited // to True. linkLabel1->LinkVisited = true; // Then do whatever other action is appropriate }