A WinForms HyperLink control
For a little project Im working on, I wanted to simulate a HyperLink control on a webform. I came up with the following control, which you may find useful.
Of course, there can be additional properties added to control your MouseOver and MouseOut colors, but Ill leave that as an excercise to the reader. The control as it sits covers my needs. :)
public class HyperLink : System.Windows.Forms.Label { private string navigateUrl; public HyperLink() { this.Cursor = Cursors.Hand; this.ForeColor = Color.Blue; } public override string Text { get { return base.Text; } set { base.Text = value; Size = new Size(PreferredWidth, PreferredHeight); } } public virtual string NavigateUrl { get { return navigateUrl; } set { navigateUrl = value; } } protected override void OnMouseEnter(EventArgs e) { this.ForeColor = Color.Red; base.OnMouseEnter (e); } protected override void OnMouseLeave(EventArgs e) { this.ForeColor = Color.Blue; base.OnMouseLeave(e); } protected override void OnMouseDown(MouseEventArgs e) { if (e.Button == MouseButtons.Left && e.Clicks == 1) { System.Diagnostics.Process.Start(navigateUrl); } base.OnMouseDown (e); } }
Enjoy!
Do you mean System.Windows.Forms.LinkLabel?
LinkLabel accomplishes the same goal. However, I found that LinkLabel was much more complex to use. The reason for that is having to define the LinkArea as character positions in the Text.
Of course, this offers some flexibility if you wish to have other items in your text outside of the link itself.
With my HyperLink control, you get a simplified (read: dumbed down) version of LinkLabel, where the entire Text is hot.
I was going to ask the same thing as Jason, thanks for the explanation. :)


