Reputation: 4104
I have this really simple block of code:
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
AddRow();
if (e.KeyCode == Keys.Back && dataGridView1.SelectedRows.Count > 0)
RemoveRow();
}
Then when I step through my code and hit either Delete
or Backspace
, this is the value of e.KeyCode:
I know my code doesn't check for Delete
but I can add that in simply afterwards.
If I cast KeyCode
to a string, it gives me the value "Delete" but I shouldn't have to do that...
edit; I should note that my first block of code, for Ctrl-V works perfectly fine. So I don't understand why e.KeyCode doesn't evaluate to Keys.Delete or Keys.Backspace?
edit 2; My coworker found something that could be 100% coincidental, or extremely interesting.
KeyCode.Delete
= 46.
KeyCode.Space
= 32
KeyCode.Back
= 8
KeyCode.MButton
= 4;
KeyCode.RButton
= 2;
32 + 8 + 4 + 2 = 46. I feel like some tinfoil hat conspiracy theorist for thinking this might be on to something, but we're completely baffled.
Upvotes: 1
Views: 2443
Reputation: 3388
From MSDN, "This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values"
What you see is string representation of the enum value. It uses Flags
attribute to construct the string in Enum.ToString()
method. See this SO question for more clarification.
Upvotes: 1
Reputation: 673
Back
is the enumeration value that indicates the backspace key was pressed (MSDN-Keys Enumeration). Example code:
if (e.KeyCode.HasFlag(Keys.Back)) {
//do stuff here
}
Upvotes: 0