When application is run, on pressing arrow keys, Left/Right/UP/Down should appear in the label. But after opening the application, the focus is on button1 and it shifts to button2 on pressing any arrow key and it continues on each arrow key press. I need something like PreventDefault() in java. The arrow key press doesn't fire the KeyDown event only. What changes should I make to make it fire?
namespace ArrowPress
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
KeyPreview = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
label1.Text = "Left";
}
if (e.KeyCode == Keys.Right)
{
label1.Text = "Right";
}
if (e.KeyCode == Keys.Up)
{
label1.Text = "Up";
}
if (e.KeyCode == Keys.Down)
{
label1.Text = "Down";
}
}
}
}
This is how my form looks like: