Hello,

Another thing, I posted another problem a minute ago, but I have another one but I think both should have simple solutions.

This problem is that I have a button that I want to add an accelerator, so I use an ampersand (&) before the key I want to use in the text property of the button. The letter is underlined and everything seems ok.

The short-cut key does not work I have tried ALT + KEY and also CTRL + KEY.

Any Ideas?

Thanks

Recommended Answers

All 8 Replies

You need to create an event on the form for press down. Look at this code:

public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            this.KeyDown += new KeyEventHandler(Form1_KeyDown);
        }

        private void buttonBegin_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button \"Begin\" has been just pressed");
        }
        
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt && e.KeyCode == Keys.B)
            {
                buttonBegin_Click(null, null);
            }
        }
    }

Ok I will do that, I thought I could use the ampersand method, it would have been faster.

Why I cannot use the ampersand method?

Thanks

What would that look like?

For instance,

Add in the text property of the button

&New

And then using Alt + N to click the button.

It actually underlines the N letter but the shortcut does not work.

Hey, you need to declare somewhere the shortcut, dont you thing. With only adding an "&" sigb infront of the button text in the designer or in the editor, will simply not do.
Take a look at my example - this is it, this is how suppose to be done.
I hope it helps.
Mitja

Ok I will do it your way, but test the thing I tell you, C# does everything for you if you add the ampersand, I don't know why this time is not working.

Thanks

I will be sincere: I hate that C# does the code instead of me, this way I cant know what is wrong, if there comes to an error. I always like to do all code possible by my self.
Its good to have that in your self too. This way nothing can surprise you. Agree?

hehe, bye
Mitja

Yes I agree, and doing the code was pretty easy now is working, but things so simple like these might save you sometime if you let visual studio do it.

Thanks for your help I will mark this as answered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.