How can I avoid adding Shift key to undo stack in TextBox_KeyDown event?

  • Thread starter Thread starter WikiGrrrl
  • Start date Start date
W

WikiGrrrl

Guest
I'm writing a C# WinForms .NET app using Undo and Redo stacks. Here's the code that handles a text box's KeyDown event...

public void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ControlKey && ModifierKeys == Keys.Control || e.KeyCode == Keys.ShiftKey) { }
else if (e.KeyCode == Keys.Z && ModifierKeys == Keys.Control)
{
if (undoStack.Count > 0)
{
StackPush(sender, redoStack);
undoStack.Pop()();
}
}
else if (e.KeyCode == Keys.Y && ModifierKeys == Keys.Control)
{
if (redoStack.Count > 0)
{
StackPush(sender, undoStack);
redoStack.Pop()();
}
}
else
{
redoStack.Clear();
StackPush(sender, undoStack);
}
}

Problem: If I type "Hello" into the text box using Shift+H for the capital H, the Shift key is the first thing added to the Undo stack separately, followed by the "H". If I type "hello" the "h" is the first thing added. I detected this problem by clicking the Undo button on my toolstrip until the Undo button becomes disabled. Here's the code for that...

public void UndoMe()
{
if (undoStack.Count > 0)
{
StackPush(txtQuestion, redoStack);
undoStack.Pop()();
}
}

I tried to eliminate this by checking for the Shift key in the first line in "TextBox_KeyDown"...

if (e.KeyCode == Keys.ControlKey && ModifierKeys == Keys.Control || e.KeyCode == Keys.ShiftKey) { }

Any ideas on how to ignore keys I don't want to add to the Undo stack?

Continue reading...
 
Back
Top