0

I am checking the keycode for a textbox, and I want a certain task to be performed when the user presses Enter.

It has been working perfectly, but the task that I am trying to perform now, usually is done using a mouse click. So on the OK on that task (a FolderBrowserDialog), it keeps calling the dialog control.

Oddly enough, even though the debugger shows me into the if branch, for e it shows {KeyData = LButton|MButton|Back}, but KeyValue is still 13...

I think it may be that the textbox remembers its last entry... True ?

In my troubleshooting, I have added a boolean variable so I only go into FolderBrowser when it is true, I have tried to add and delete a space from the textbox after the Browse, and even clear the textbox... Each attempt seemed to make things worse.

It seemed that I was in a quasi-infinite loop - yet it would go away after lots of "ok"'s, and stepping through, I found that for every letter I type in the textbox, I spend 4 to 5 rounds in the CheckKeys. I don't understand why... Or how to fix it.

I added a "e.Handled" which did me no good.

Here's the code:

private void txtDir_TextChanged(object sender, EventArgs e)
{
  this.txtDir.KeyUp += new System.Windows.Forms.KeyEventHandler(CheckKeys);
}

private void CheckKeys(object sender, System.Windows.Forms.KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
      if (sender == txtDir && txtDir.Text != "" && System.IO.Directory.Exists(txtDir.Text))
      {
          btnBrowse_Click(this, e);
      }
    }           
}

Why am I going through this check so many times ? Can I add a different test ? Am I doing something wrong ? (nothing is set as default action, for form or textbox...)

Thank you.

Thalia
  • 13,637
  • 22
  • 96
  • 190

3 Answers3

1

In your code you added handler for KeyUp at TextChanged Event. so, When TextChanged new handler will be added for KeyUp Event. Thats why multiple time each letter is checked. put Handler at Form load event.

e.g. If I have entered five letter in TextBox so, 5 Handler will be added for KeyUp. means Number of KeyUp Event Handler equals to number of time TextChanged Event called.

this.txtDir.KeyUp += new System.Windows.Forms.KeyEventHandler(CheckKeys); 

this add new handler for KeyUp event. So, when this line execute new handler will be added.

By putting Handler at Form Load event, you can solve multiple letter checked problem.

Try with,

private void FormLoad(object sender, EventArgs e)
{
         this.txtDir.KeyUp += new System.Windows.Forms.KeyEventHandler(CheckKeys);
}

private void CheckKeys(object sender, System.Windows.Forms.KeyEventArgs e)
{
  if (e.KeyCode == Keys.Enter)
  {
  if (sender == txtDir && txtDir.Text != "" && System.IO.Directory.Exists(txtDir.Text))
  {
      btnBrowse_Click(this, e);
  }
  }           
}

And one more thing As I understand your code, you want to execute btnBrowse_Click if Enter pressed in TextBox control. But Enter key not handled with KeyUp event you need KeyDown Event handler to handle Enter key.

Code:

private void FormLoad(object sender, EventArgs e)
{
         this.txtDir.KeyDown += new System.Windows.Forms.KeyEventHandler(CheckKeys);
}

private void CheckKeys(object sender, System.Windows.Forms.KeyEventArgs e)
{
  if (e.KeyCode == Keys.Enter)
  {
  if (sender == txtDir && txtDir.Text != "" && System.IO.Directory.Exists(txtDir.Text))
  {
      btnBrowse_Click(this, e);
  }
  }           
}
Jignesh Thakker
  • 3,638
  • 2
  • 28
  • 35
1

Use KeyDown event instead of TextChanged and write down e.Handle = True in it. write down following code in your textBox.KeyDown event:

if (e.KeyCode == Keys.Enter)
{
   if (sender == txtDir && txtDir.Text != "" && System.IO.Directory.Exists(txtDir.Text))
   {
       e.Hanlde = true; //it will be close enter keydown handling at this time
       btnBrowse_Click(this, e);
   }
}    
Hamid
  • 817
  • 1
  • 13
  • 29
0

Replaced TextChanged with KeyDown directly and it works again !

(on a side note, I still don't understand why each letter is tested several times...)

Edit: Now after reading the accepted answer, I do understand...

Thalia
  • 13,637
  • 22
  • 96
  • 190