2

I have more or less the same problem as this guy but his post is 2 years old, so I thought I could open a new one.

In the program I use labels and as I found out mnemonics on labels trigger the enter event of the next control in the tab order. So implemented click and enter methods. But here is the problem. I created a test program. The program persists out of two labels, a button and a textbox.

test program layout

test program tab order

Second label is just to control if the enter event is fired. When I hit ALT the underscore appears fine but when I press the second key (for Reset) nothing happens. Furthermore if the underscore appears and I press the ALT key again he doesn't disappear and the button totally ignores if ALT is pressed or not. I moved to an other pc with VisualStudio 2013 but got the same result. I downloaded the VisualStudio 2017, tried to create new program => does not work either.

English is not the language I know the best, so I'm glad if you can give me a hint when I wrote something wrong. I hope someone can help me.

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void reset_Click(object sender, EventArgs e)
        {
            textBox.Text = "";
        }

        private void button_Click(object sender, EventArgs e)
        {
            textBox.Text = "Button";
        }

        private void nothing_Enter(object sender, EventArgs e)
        {
            textBox.Text = "nothing";
        }
    }
}

Form1.Designer.cs

        this.reset.Click += new System.EventHandler(this.reset_Click);
        this.button.Click += new System.EventHandler(this.button_Click);
        this.nothing.Enter += new System.EventHandler(this.nothing_Enter);
Rish
  • 1,303
  • 9
  • 22
John
  • 23
  • 5
  • 2
    That is way too much text. Please shorten your post to a an absolute minimum, provide only the core information to understand and rebuild your problem. – L. Guthardt Oct 30 '17 at 09:44
  • Ok I will do that. – John Oct 30 '17 at 09:45
  • Is it now better? – John Oct 30 '17 at 09:54
  • Yes it´s way better now. – L. Guthardt Oct 30 '17 at 09:57
  • 1
    Labels were designed to act as mnemonic catchers for their "buddy" control. Implemented by Label.ProcessMnemonic(). Primary useful to help give a TextBox focus. This label doesn't have a buddy. Consider overriding ProcessCmdKey() to recognize the keystroke. – Hans Passant Oct 30 '17 at 11:27
  • With buddy. Do you mean something like a textbox that comes in tab order after the label? – John Oct 30 '17 at 14:10
  • Exactly what are you trying to achieve with the mnemonics - they do not seem to be doing what you want - so if you explained that then maybe we could point you in the right direction. – PaulF Oct 30 '17 at 14:22
  • Ok let me try. In a lot of programs like Microsoft Word or Windows Explorer you can hit the ALT key to use your keyboard instead of your mouse. To open files, save files, change view, etc. I want to realize that in the program. – John Oct 30 '17 at 14:37
  • Those sound more like Menu shortcuts, rather than control mnemonics - so maybe this is what you should be looking at : http://www.homeandlearn.co.uk/csharp/csharp_s4p1.html or this : https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/walkthrough-providing-standard-menu-items-to-a-form – PaulF Oct 30 '17 at 14:44
  • Ok thank you I will do that! – John Oct 30 '17 at 14:49

1 Answers1

2

When you use a mnemonic on a label - the focus goes to the next selectable object on the form with a HIGHER tab index (it doesn't wrap around to the next selectable control as tab does). As labels are not selectable by default - then you have no selectable control after your Reset label - so nothing appears to happen.

If you add another selectable control with higher tab index - say a textbox with tab index value set to 4 - then you will find pressing Alt-r will go to that control as you expect.

If you want to make the labels selectable then you can create a derived class as shown in the answer here : Make label participate in control tabbing - if you make the "nothing" label a SelectableLabel - then pressing Alt-r will cause focus to move to that label.

PaulF
  • 6,673
  • 2
  • 18
  • 29
  • Thank you for your answer. I did as you and the post described. It worked but just for one time. It seems you can only hit ALT+R once. After that I need to click the button with the mouse to use ALT+R again. – John Oct 30 '17 at 13:04
  • The mnemonic only applies to the control it is set on - so Alt-r will only move you from the "Reset" label to the "nothing" label (assuming you made it a SelectableLabel) - after pressing alt-r, pressing alt-r multiple times will appear to have no effect because you keep moving to the "nothing" label where you already are. If you tab to the button or shift-tab back to the "Reset" label (or select a control by clicking) & press Alt-r then you will move to the "nothing" label again. – PaulF Oct 30 '17 at 13:44
  • So even if I press ALT+B after I pressed ALT+R the focus rests on the nothing label? – John Oct 30 '17 at 14:07
  • The mnemonics work slightly differently for the different types of control - see Hans Passant's comment above that the Label mnemonic was designed to be paired with another selectable control immediately following so if you had a label next to a textbox (in tab index order) the mnemonic would move you to that control. With the button, the effect of the mnemonic is to cause a Click event - BUT the focus does not move from where it currently is. Try putting the cursor in the textbox in the middle of some text & press Alt-B - the button click event will fire, but the cursor wont move. – PaulF Oct 30 '17 at 14:19
  • Ok I can fix that with a button.Focus(); after the textbox.Text = "nothing"; I understand now that labels are not made for something like that. – John Oct 30 '17 at 14:31