0

I have a simple increment on textbox by pressing down arrow key which are as below.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{

     if (keyData == Keys.Down)
     {

           int c = int.Parse(textBox1.Text);
           c++;
           textBox1.Text = c.ToString();


     }
 }

The above works on pressing double down arrow key instead of single pressing down arrow key.

Note: The above code is on UserControl. And I have tried it on simple winform application on form keydown EventHandller and the same is works fine.

How to overcome?.

Mahesh Wagh
  • 151
  • 1
  • 13
  • What do you mean "the same is works fine"? I'm curious because you say you used the KeyDown handler of the textbox, which is the correct method to use. – B L Jan 18 '13 at 17:35
  • 1
    The method returns a bool. Return true if you used the key. – Hans Passant Jan 18 '13 at 17:55
  • @HansPassant, Yes you are absolutely wright I agreed with you just need to return value of bool as it is bool method. – Mahesh Wagh Jan 18 '13 at 18:19

1 Answers1

3

You'll need to handle other commands that existed before and return when you handle ones you are looking for. Try changing it to this and see if that helps:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
   if (msg.WParam.ToInt32() == (int)Keys.Down)
   {
      int c = int.Parse(textBox1.Text);
      c++;
      textBox1.Text = c.ToString();
      return true;
   }
   return base.ProcessCmdKey(ref msg, keyData);
}
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • yes your trick is works and I also agreed with Mr. HansPassant that there are only need to return true on use of key as the above method return bool. – Mahesh Wagh Jan 18 '13 at 18:17
  • @MaheshWagh - Glad I could help, dont forget to accept the answer if it is what you were looking for – SwDevMan81 Jan 18 '13 at 21:04