Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Im working on a project and my instructor wants the exit button to be assigned the shortcut alt-x. So far i have
C#
         private void Form1_Load(object sender, EventArgs e)
         {
             radAddition.Checked = true;
             radSmallRange.Checked = true;
             picCorrect.Visible = false;
             picWrong.Visible = false;
             this.KeyPreview = true;

         }
private void Form1_KeyDown(object sender, KeyEventArgs e)
         {
            if (e.Modifiers == Keys.Alt && e.KeyCode == Keys.X)
            {
                this.btnExit.PerformClick();
            }
         }
private void btnExit_Click(object sender, EventArgs e)
         {
             DialogResult result;
             result = MessageBox.Show("Are you sure you want to exit?",
                 "Exiting",
                 MessageBoxButtons.YesNo);
             if (result == DialogResult.Yes)
             {
                 this.Close();
             }
         }

I just copied and pasted the stuff so not everything is relevant but everything relevant is there. But when i press alt-x noting happens! what am i doing wrong? thanks in advance!
Posted
Comments
BacchusBeale 30-Mar-15 17:38pm    
Did you register the event in the form? e.g this.KeyDown+=Form1_KeyDown
Member 11561872 30-Mar-15 17:44pm    
Actually the below solution worked with enetering "E&xit" into the text property. However i do not know what registering the event does. I just did what i could find based on online research.

1 solution

You created the event-handler for the KeyDown event, but we cannot see if you set this handler in the event's delegate list.
Moreover, as you want to use a shortcut with ALT key, there may be a simpler way to accomplish what you want to do.

Using your event handler:

Just insert this statement in your form's constructor:
C#
this.KeyDown += Form1_KeyDown;


You may also create a specialized method ConfirmExit, which you would call from both your event-handlers (in general, I really don't like direct implementations in event handlers).
C#
private void ConfirmExit()
{
   DialogResult result = MessageBox.Show("Are you sure you want to exit?", "Exiting", MessageBoxButtons.YesNo);
   if (result == DialogResult.Yes)
   {
      this.Close();
   }
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.Alt && e.KeyCode == Keys.X)
   {
      ConfirmExit();
   }
}

private void btnExit_Click(object sender, EventArgs e)
{
   ConfirmExit();
}

One implementation, used in two different places. Clean and easy to read/follow.

Using a much simpler solution:
As you want to use the ALT key, you may use an in-built functionnality: in your button's Text property, prefix the letter you want to use as shortcut (the X key) with the ampersand sign (&).
Example:
C#
btnExit.Text = "E&xit";

You can do that in design mode from the property grid.
The letter X will be underlined, meaning that you can activate the button from keyboard by pressing ALT + X.
Of course, this is not a solution if:
- you want another combination key than ALT;
- or the letter you want as shortcut does not appear in the button's Text property.

Hope this helps.

Edited: simplified alt test in KeyDown handler.
 
Share this answer
 
v5
Comments
Member 11561872 30-Mar-15 17:41pm    
I was not aware I could simply use "&" in designer mode to do this. I worked though. Thanks!
phil.o 30-Mar-15 17:44pm    
You're welcome.
George Swan 30-Mar-15 17:51pm    
This is a good well-written solution +5. You can further simplyfy the 'if' statement as: if (e.Alt && e.KeyCode == Keys.X)
phil.o 30-Mar-15 17:56pm    
I was not aware of the Alt boolean property of KeyEventArgs. I don't do much UI coding.
Thanks for pointing it out :)
Afzaal Ahmad Zeeshan 30-Mar-15 20:33pm    
+5 for the design mode method of doing this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900