How to minimize by pressing esc(c#)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • desturrr
    New Member
    • Oct 2009
    • 18

    How to minimize by pressing esc(c#)

    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(o bject 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
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    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?

    Code:
    this.WindowState = FormWindowState.Minimized;

    Comment

    • desturrr
      New Member
      • Oct 2009
      • 18

      #3
      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 ?

      Comment

      • desturrr
        New Member
        • Oct 2009
        • 18

        #4
        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.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          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

          Comment

          • Saser
            New Member
            • Jul 2009
            • 17

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

            Code:
            private void Form1_KeyPress(object sender, KeyPressEventArgs e)
                    {
                        if (e.KeyChar == (char)Keys.Escape)
                        {
                            this.WindowState = FormWindowState.Minimized;
                        }
                    }

            Comment

            Working...