title: c# Textbox KeyEventArgs - each letter is checked multiple times
body: <p>I am checking the keycode for a textbox, and I want a certain task to be performed when the user presses <code>Enter</code>. </p>
  
  <p>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 <code>FolderBrowserDialog</code>), it keeps calling the dialog control. </p>
  
  <p>Oddly enough, even though the debugger shows me into the if branch, for e it shows <code>{KeyData = LButton|MButton|Back}</code>, but <code>KeyValue</code> is still 13... </p>
  
  <p>I think it may be that the textbox remembers its last entry... True ?</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>I added a "e.Handled" which did me no good.</p>
  
  <p>Here's the code:</p>
  
  <pre><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 &amp;&amp; txtDir.Text != "" &amp;&amp; System.IO.Directory.Exists(txtDir.Text))
        {
            btnBrowse_Click(this, e);
        }
      }           
  }
  </code></pre>
  
  <p>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...)</p>
  
  <p>Thank you.</p>