C#:how to make Esc key to close form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ssknov
    New Member
    • Dec 2007
    • 40

    C#:how to make Esc key to close form

    hi
    i need to close the form when esc key is pressed. i tried as below but no response.

    Code:
    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if(e.KeyChar==(char)27)
                {
                    this.Hide();
                }
            }
    thanks in adv
    kssk
  • mzmishra
    Recognized Expert Contributor
    • Aug 2007
    • 390

    #2
    If you are closing the form try close() method

    Comment

    • ssknov
      New Member
      • Dec 2007
      • 40

      #3
      Originally posted by mzmishra
      If you are closing the form try close() method
      ya
      thats right.

      but when i tried as

      this.close(); //also it doesnt responds my Esc key press.

      ssk

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        Try to handle that in the KeyDown event and also set the KeyPreview property of the form to True.

        Comment

        • vksingh24
          New Member
          • Dec 2007
          • 21

          #5
          Originally posted by ssknov
          hi
          i need to close the form when esc key is pressed. i tried as below but no response.

          [code=vbnet]
          private void Form1_KeyPress( object sender, KeyPressEventAr gs e)
          {
          if(e.KeyChar==( char)27)
          {
          this.Hide();
          }
          }[/code]
          thanks in adv
          kssk

          I tried this:

          [CODE=vbnet]using System;
          using System.Collecti ons.Generic;
          using System.Componen tModel;
          using System.Data;
          using System.Drawing;
          using System.Text;
          using System.Windows. Forms;

          namespace test
          {
          public partial class Form2 : Form
          {
          public Form2()
          {
          InitializeCompo nent();
          }


          private void Form2_KeyDown(o bject sender, KeyEventArgs e)
          {
          if (e.KeyValue==27 )
          {
          this.Hide();
          }
          }

          private void Form2_Load(obje ct sender, EventArgs e)
          {

          Form2.ActiveFor m.KeyPreview = true;

          }

          }
          }[/CODE]

          Also set the

          [CODE=vbnet]this.KeyDown += new System.Windows. Forms.KeyEventH andler(Form2_Ke yDown);[/CODE]

          in the InitializeCompo nent() of the form
          Last edited by debasisdas; Dec 27 '07, 11:51 AM. Reason: Formated using code=vbnet tags.

          Comment

          • mkumar20xx
            New Member
            • Nov 2007
            • 2

            #6
            Just Try This and Let Me Know.

            Vb Code
            ------------
            [code=vbnet]
            Protected Overrides Function ProcessCmdKey(B yRef msg As System.Windows. Forms.Message, ByVal keyData As System.Windows. Forms.Keys) As Boolean
            Try
            If msg.WParam.ToIn t32 = Convert.ToInt32 (Keys.Escape) Then
            Me.Close()
            Else
            Return MyBase.ProcessC mdKey(msg, keyData)
            End If

            Catch ex As Exception
            Return False
            End Try
            Return MyBase.ProcessC mdKey(msg, keyData)
            End Function
            [/code]

            C# Code
            ------------
            [code=c#]
            protected override bool ProcessCmdKey(r ef System.Windows. Forms.Message msg, System.Windows. Forms.Keys keyData)
            {
            try
            {
            if (msg.WParam.ToI nt32() == (int)Keys.Escap e)
            {
            this.close();
            }
            else
            {
            return base.ProcessCmd Key(ref msg, keyData);
            }
            }
            catch (Exception Ex )
            {
            MessageBox.Show ("Key Overrided Events Error:"+Ex.Mess age);
            }
            return base.ProcessCmd Key(ref msg,keyData);
            }
            [/code]

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Basically if the Form1 is not the control that has focus, then the keypress isn't being seen, so its not repsonding. maybe a textbox has focus.

              Handling the Window Messaging as mkumar20xx has shown looks to be the better approach. While he gave you a solution, I thought it would be good to *understand* why it was a solution and why your approach wasn't working for you.

              Comment

              Working...