1

I want some of my linklabels to have padding with a BackColor, kind of like this:

Click here

But the problem is that you can't really have a padded linklabel which can be clicked, you'd have to click just the text only (the click doesn't register if you click within the padded area.)

So another option is to have a Panel with the linklabel inside and then register the Click event for both linklabel and panel controls in order to get that clickable button effect.

How can we:

  • Make a linklabel inside a panel and have either of them respond to click event _without having to register to click event for both controls; or:
  • Have a LinkLabel with padding of 10px all the way around and make the linklabel entirely clickable?
Mothy
  • 406
  • 1
  • 7
  • 19
jAsOn
  • 927
  • 2
  • 7
  • 16

2 Answers2

3

In fact, a LinkLabel can contain many Links, for your requirement (can click on the background), we have to use the LinkLabel for only 1 link because all the links have the same background area, clicking on the background area can't tell us which link is clicked. To handle clicking on each link, we handle the event LinkClicked, but to change its behavior by allowing user to click on the whole background area, we have to handle the event Click as normally. Add some MouseEnter and MouseLeave handler to change the backcolor if needed. Here is the code:

//Setup the link data for the LinkLabel
linkLabel1.Links.Add(new LinkLabel.Link() {Description = "StackOverflow", LinkData = "http://www.stackoverflow.com"});
linkLabel1.Text = "Stackoverflow";
linkLabel1.BackColor = Color.LightGray;
//Add 10px padding around the link text 
linkLabel1.Padding = new Padding(10);
//Do this to change the Cursor to Hand pointer when mouse over the whole link
linkLabel1.Cursor = Cursors.Hand;
//Click event handler for your linkLabel1
private void linkLabel1_Click(object sender, EventArgs e) {
  //Try showing the URL which the link refers
  //we can use this info to, for example, visit the link
  MessageBox.Show(linkLabel1.Links[0].LinkData.ToString());
}
//MouseEnter event handler to change the BackColor accordingly
private void linkLabel1_MouseEnter(object sender, EventArgs e) {
  linkLabel1.BackColor = Color.Yellow;
}
//MouseLeave event handler to change the BackColor accordingly
private void linkLabel1_MouseLeave(object sender, EventArgs e){
  linkLabel1.BackColor = Color.LightGray;
}

NOTE: By customizing this way, a Label can replace the LinkLabel, we just need some suitable Font, TextAlign, Tag (for LinkData) ...

King King
  • 61,710
  • 16
  • 105
  • 130
  • What the?! Is this documented? I've never seen this in the docs. I felt like I've just stepped outside for the first time in a hundred years when you said "a linklabel can contain many links". – jAsOn Aug 31 '13 at 16:02
  • 1
    @jAsOn yes, look at the `Links` property of a `LinkLabel`. It contains many `LinkLabel.Link` which has 1 constructor like this `LinkLabel.Link(int start, int length, object linkData)`, where `start` is the start index of the link in the **Text** and `length` is the length of the link. The links can't overlap each other. You can search for more and try it yourself. A `LinkLabel` has one `Text` but we can specify which parts of text to be treated as separate links. Plz refer this http://msdn.microsoft.com/en-us/library/system.windows.forms.linklabel.links.aspx – King King Aug 31 '13 at 16:10
1

You can make the padded linklabel clickable by using the "Click" event of linklabel rather than using the "LinkClicked event".

private void linkLabel1_Click(object sender, EventArgs e)
        {

             //Your code here
            MessageBox.Show("Clicked Me");
        }
Mothy
  • 406
  • 1
  • 7
  • 19