Connecting Tech Pros Worldwide Forums | Help | Site Map

How to minimize by pressing esc(c#)

Newbie
 
Join Date: Oct 2009
Posts: 8
#1: 4 Weeks Ago
I am currently working on a windows form and i want to minimize the form when pressed the escape key , i tried the cancelButton property for form ,and bind the button which is

private void btnHide_Click(object sender, EventArgs e)
{
Hide();
}

pressing the button by mouse hides the form, but pressing esc which is bound with btnHide is not working ,

Is there sth that i am missing ?

Thanks.

tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,791
#2: 4 Weeks Ago

re: How to minimize by pressing esc(c#)


A control must have focus for the keypress to go to that control.
You wouldn't want to be in a textbox and hit a key assigned to a button and just have the program take off in an unexpected way.

Your first line says you want to minimize the form. But you are calling the hide method. Did you want to minimize or did you want to hide?

Expand|Select|Wrap|Line Numbers
  1. this.WindowState = FormWindowState.Minimized;
Newbie
 
Join Date: Oct 2009
Posts: 8
#3: 4 Weeks Ago

re: How to minimize by pressing esc(c#)


Ok, i understand the problem now, i made the button visible property false , in order not to see it on the form and expect it to work when pressed the escape key.The other problem is now , is there have to be a shown button in order to work this escape key which is bound to that button ?
Newbie
 
Join Date: Oct 2009
Posts: 8
#4: 4 Weeks Ago

re: How to minimize by pressing esc(c#)


Yeah i did it,I made the button so small and put where nobody sees it :), but if you have any other solution , you are welcome.

Thanks.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,791
#5: 4 Weeks Ago

re: How to minimize by pressing esc(c#)


If you minimize instead of hide you don't have to worry about needing a way to show it.

You might want to look at the NotifyIcon object. That way you can hide or minimize or whatever and put commands in a menu in tasktray icon
Newbie
 
Join Date: Jul 2009
Posts: 17
#6: 4 Weeks Ago

re: How to minimize by pressing esc(c#)


Not sure if it's this you're looking for, but anywayz.

Expand|Select|Wrap|Line Numbers
  1. private void Form1_KeyPress(object sender, KeyPressEventArgs e)
  2.         {
  3.             if (e.KeyChar == (char)Keys.Escape)
  4.             {
  5.                 this.WindowState = FormWindowState.Minimized;
  6.             }
  7.         }
Reply