Posted By: Shark_M | Aug 27th, 2006 @ 4:55 AM
page 1 of 1
Comments: 2 | Views: 5806
OKay, quick question.

Make a windows forms app, in C#. Add a ToolStrip, in the tool strip create a textbox. Now here is the problem. Put the breakpoint on the event KeyPress or any other key event.

Run it, Press Escape key. No event is triggered.



Now the Escape key remove the focus from the textbox (you dont see the cursor inside the box). But no event is triggered for KeyChar 27 or Keys.Escpae. Why? Is this a bug?
Escape Key is a command key, so you should achieve this with another trick, for example:

<code>

using System;
using System.Windows.Forms;

namespace HandleEscapeKeyDemo
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        protected override Boolean ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                OnPressEscapeKey();
            }

            return true;
        }

        private void OnPressEscapeKey()
        {
            MessageBox.Show("Escape Key Is Pressed");
        }
    }
}

</code>

Sheva


page 1 of 1
Comments: 2 | Views: 5806