开发者

How to raise ButtonClick event by using mnemonic of a label?

开发者 https://www.devze.com 2023-02-28 02:16 出处:网络
In my C# app I have a label with a mnemonic (e.g. &Path) and a button. I want to raise ButtonClick event when user presses mnemonic for the label (e.g. [Alt] and then [P]). But I haven\'t found an

In my C# app I have a label with a mnemonic (e.g. &Path) and a button. I want to raise ButtonClick event when user presses mnemonic for the label (e.g. [Alt] and then [P]). But I haven't found any label even开发者_如何学Pythont to handle this situation. Using OnFocus event of a button is not an option, as user may use [Tab] key to navigate.

So is there a way to achieve what I want?

Thanks in advance.


or you can just name your button with something that starts with the litter p and then put & before it, the alt + p will trigger the btn_Click event handler

Edit: What about something like this :)

How to raise ButtonClick event by using mnemonic of a label?


Mnemonics on labels only gives focus to the control that has the next TabIndex and that's all it does. You cannot use it to directly invoke anything (such as the click event of a button).

You can use the knowledge of this behavior to simulate what you want to achieve. The idea is to place a lightweight, focusable control on your form that has a TabIndex that immediately follows the label but in a location that it is not visible (like the beyond the upper left corner). Then do what you will on that hidden control's focus events.

Here's a complete standalone example. In this case, the hidden control will be a check box.

using System;
using System.Drawing;
using System.Windows.Forms;

public class MyForm : Form
{
    public MyForm()
    {
        targetLabel = new Label()
        {
            Text = "&Label",
            TabIndex = 10,
            AutoSize = true,
            Location = new Point(12, 17),
        };
        // you don't need to keep an instance variable
        var hiddenControl = new CheckBox()
        {
            Text = String.Empty,
            TabIndex = 11,                    // immediately follows target label
            TabStop = false,                  // prevent tabbing to control
            Location = new Point(-100, -100), // put somewhere not visible
        };
        hiddenControl.GotFocus += (sender, e) =>
        {
            // simulate clicking on the target button
            targetButton.Focus();
            targetButton.PerformClick();
        };
        targetButton = new Button()
        {
            Text = "&Click",
            TabIndex = 20,
            AutoSize = true,
            Location = new Point(53, 12),
        };
        targetButton.Click += (sender, e) =>
        {
            MessageBox.Show("Target Clicked!");
        };
        dummyButton = new Button()
        {
            Text = "&Another Button",
            TabIndex = 0,
            AutoSize = true,
            Location = new Point(134, 12),
        };
        dummyButton.Click += (sender, e) =>
        {
            MessageBox.Show("Another Button Clicked!");
        };

        this.Controls.Add(targetLabel);
        this.Controls.Add(hiddenControl);
        this.Controls.Add(targetButton);
        this.Controls.Add(dummyButton);
    }
    private Label targetLabel;
    private Button targetButton;
    private Button dummyButton;

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MyForm());
    }
}


You didnt specify type of your project (Winforms/WPF) but I think the solution would be same for all this types:
You should set KeyPreview on your form to true and check pressed keys in KeyUp event handler as nelow:

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.P && e.Alt == true)
        {
            MessageBox.Show("Got it");
        }
    }

In this sample you would get message box in case of Alt+P pressed

0

精彩评论

暂无评论...
验证码 换一张
取 消