Hi,
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).
If users use the shortcut keys (Ctrl+Z), it's perfect. But what if the users click a ToolStripButton?
How can I programmatically simulate a KeyDown Event for RichTextBox in C# 2010.
Here is the code snippet that has some issues. Can you help me on how to Simulate/RaiseEvent in C#?
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 & 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;
}
}
}
Thanks.