Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using keyboard shortcuts with LinkLabel controls

I've noticed that keyboard-shortcuts assigned to linklabel controls in standard .NET WinForms forms are not functioning.

I have created a LinkLabel control instance and assigned the Text property to be "Select &All". For most controls (label, button, radio button, etc) this would cause Alt+A to become the designated keyboard shortcut to trigger the default event (Clicked). This is not happening for the LinkLabel (though it is working okay for other controls)

  • I have verified that the keyboard shortcut is not a duplicate.
  • I have checked to see if the shortcut is setting the focus rather than triggering Clicked. The focus remains unchanged.
  • I have verified that the UseMnemonic property is set to true.

Any ideas?


Solution

Thank you Charlie for the correct answer. Exactly what I needed. I made a slight modification since this code snippet wouldn't compile as-is. LinkLabelLinkClickedEventArgs requires a LinkLabel.Link as a construction parameter rather thank a LinkLabel.

class LinkLabelEx : LinkLabel
{
    protected override bool ProcessMnemonic(char charCode)
    {
        if (base.ProcessMnemonic(charCode))
        {
            if (this.Links.Count == 0)
                return false;
            OnLinkClicked(new LinkLabelLinkClickedEventArgs(this.Links[0]));
            return true;
        }
        return false;
    }
}
like image 286
Simon Gillbee Avatar asked Mar 25 '09 04:03

Simon Gillbee


People also ask

How to use Link label c#?

To create a LinkLabel control at design-time, you simply drag and drop a LinkLabel control from Toolbox to a Form. After you drag and drop a LinkLabel on a Form. The LinkLabel looks like Figure 1. Once a LinkLabel is on the Form, you can move it around and resize it using mouse and set its properties and events.

What is Link label?

A link label is like a hyperlink that you'd see on a webpage. It's a label thats blue and has an underline and, when clicked, can point to a URL. A label is just a label.


1 Answers

I believe this is just a shortcoming of LinkLabel; it doesn't generate a click event when you use its mnemonic. However, I've used the following code as a workaround with good success:

class BetterLinkLabel : LinkLabel
{
  protected override bool ProcessMnemonic( char charCode )
  {
    if( base.ProcessMnemonic( charCode ) )
    {
      // TODO: pass a valid LinkLabel.Link to the event arg ctor
      OnLinkClicked( new LinkLabelLinkClickedEventArgs( null ) );
      return true;
    }
    return false;
  }
}
like image 143
Charlie Avatar answered Nov 03 '22 16:11

Charlie