开发者

WinForms KeyDown event losing keyboard input on first usage

I'm trying to make a WinForms program such that a TextBox is normally hidden (Visible = false) until the user starts typing on the keyboard, at which point the TextBox should become visible and the keyboard input should go into the TextBox.

Here's the program, reduced to the essential parts:

using System.Windows.Forms;

namespace TestTextEditPopup
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();

         this.KeyPreview = true;
         textBox1.Visible = false;
      }

      protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
      {
         if (keyData != Keys.Escape)
            return base.ProcessCmdKey(ref msg, keyData);

         textBox1.Visible = false;
         return true;  // Key has been processed
      }

      private void Form1_KeyDown(object sender, KeyEventArgs e)
      {
         textBox1.Visible = true;
         textBox1.Focus();
      }
   }
}

And here's a series of screen shots:

WinForms KeyDown event losing keyboard input on first usage

First I hit "a" on the keyboard. This does cause the TextBox to become visible and get focus, but the "a" is apparently lost somewhere.

Then I hit Esc. This correctly makes the TextBox invisible again.

Then I hit "b". This time (and for all, or at least almost all subsequent times) it works - the TextBox becomes visible, gets focus, and the keyboard input is not lost - it shows up in the TextBox.

Any suggestions as to why it doesn't work the first time? Or alt开发者_运维百科ernative methods of accomplishing what I'm trying to do?

Thanks.

Edit: Just adding KeyPress as an additional tag.


Use the KeyPress event on the form instead; then you get access to character data, so you can add the character to the TextBox control, and then focus it:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (this.ActiveControl != textBox1)
    {
        textBox1.Visible = true;
        textBox1.Focus();
        textBox1.Text += e.KeyChar;
        textBox1.Select(textBox1.Text.Length, 0);
        e.Handled = true;
    }                
}


From the docs for KeyPreview

When this property is set to true, the form will receive all KeyDown (etc) events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus.

When the form starts, the textbox control is disabled and hence does not have focus.

As when the first key is pressed the textbox control does not have the focus, the keypress is lost as the form cannot process it: on subsequent keypresses, the control receives the input as it still has focus - I'm guessing it is the only control on the form, because if there was another control the focus would shift to it when the textbox became disabled, and subsequent keypresses would disappear like the initial one.

Another way of doing this would be to start off with the textbox outside the form boundaries and shift it in and out rather than make it visible / invisible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜