1

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");      
    }
Community
  • 1
  • 1
Scott
  • 11
  • 3
  • This can be worked around but the "hundreds of screens" scenario makes most any solution unacceptable. You'll need to explain what sort of bomb you set off that suddenly made those hundreds of screens misbehave. Surely there's a better way to avoid lighting the fuse on that bomb. – Hans Passant Sep 15 '15 at 13:48
  • Agreed - I'm looking for a central solution instead of one that requires a change to each screen or button. This is a VB.Net [MDI](https://msdn.microsoft.com/en-us/library/ms973874.aspx) app, and I have a way to add handlers to every button on a screen when it opens. If the test scenario above works, it will scale to the actual app easily. – Scott Sep 15 '15 at 14:28

0 Answers0