In VB.Net, buttons can be given an Access Key based on their text. For example "&Open" will make a button click when the user presses Alt+O.
However the Alt key isn't required, which is a problem for me. If the user presses "O", I don't want the Open button to click.
Based on this question, it appears I can use the AccessKeyPressedEvent, but I'm not having any luck.
This is my code, in a simple test app with two buttons titled "&AClick" and "&BClick"
Public Sub New()
InitializeComponent()
EventManager.RegisterClassHandler(GetType(UIElement), AccessKeyManager.AccessKeyPressedEvent, New AccessKeyPressedEventHandler(AddressOf CustOnAccessKeyPressed))
End Sub
Sub CustOnAccessKeyPressed(ByVal sender As Object, ByVal e As AccessKeyPressedEventArgs)
MessageBox.Show("AccessKeyPressed")
End Sub
I never see the messagebox or hit a breakpoint on CustOnAccessKeyPressed.
My scenario is a VB.Net 4.0 application with hundreds of screens. I have a central location where I can add event handlers to the buttons or the form itself as it opens. I do not want to change the existing Click events on each screen.
Edit: Same (lack of) results in a C#.Net Windows Forms app:
public Form1()
{
InitializeComponent();
EventManager.RegisterClassHandler(typeof(UIElement), AccessKeyManager.AccessKeyPressedEvent, new AccessKeyPressedEventHandler(OnAccessKeyPressed));
}
private static void OnAccessKeyPressed(object sender, AccessKeyPressedEventArgs e)
{
MessageBox.Show("OnAccessKeyPressed");
}