title: Programmatically Simulate a KeyDown Event for RichTextBox in C# 2010
body: <p>I have a RichTextBox on my form, and I want to use the default behavior as the RichTextBox does, such as, Ctrl+Z (Undo) or other actions (Ctrl+Y, Ctrl+X, Ctrl+V).</p>
  
  <p>If users use the shortcut keys (Ctrl+Z), it's perfect. But what if the users click a ToolStripButton? </p>
  
  <p>How can I programmatically simulate a KeyDown Event for RichTextBox in C# 2010.</p>
  
  <p>Here is the code snippet that has some issues.  Can you help me on how to Simulate/RaiseEvent in C#?</p>
  
  <pre><code>private void tsbUndo_Click(object sender, EventArgs e)
  {
      rtbxContent_KeyDown(rtbxContent, new KeyEventArgs(Keys.Control | Keys.Z));
  }
  
  private void tsbPaste_Click(object sender, EventArgs e)
  {
      DoPaste();
  }
  
  private void DoPaste()
  {
      rtbxContent.Paste(DataFormats.GetFormat(DataFormats.UnicodeText));
  }
  
  private void rtbxContent_KeyDown(object sender, KeyEventArgs e)
  {
      //if ((Control.ModifierKeys &amp; Keys.Control) == Keys.Control)
      if (e.Control)
      {
          switch (e.KeyCode)
          {
              // I want my application use my user-defined behavior as DoPaste() does
              case Keys.V:
                  DoPaste();
                  e.SuppressKeyPress = true;
                  break;
  
              // I want my application use the default behavior as the RichTextBox control does
              case Keys.A:
              case Keys.X:
              case Keys.C:
              case Keys.Z:
              case Keys.Y:
                  e.SuppressKeyPress = false;
                  break;
  
              default:
                  e.SuppressKeyPress = true;
                  break;
          }
      }
  }
  </code></pre>
  
  <p>Thanks.</p>