简体   繁体   中英

Windows.Forms Mnemonics don't work

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.

测试程序布局

测试程序选项卡顺序

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM