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)
Any ideas?
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;
}
}
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.
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With