Reputation: 8636
I have taken a link button on my form and on KeyDown
event I write as follows to move the link button to left
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
linkLabel1.Left = linkLabel1.Left + 5;
}
}
But this is not moving the linklabel as per required, can some one tell where I went wrong
This too didn't work
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Left:
linkLabel1.Left = linkLabel1.Left + 5;
break;
default:
return;
}
}
Upvotes: 0
Views: 1462
Reputation: 707
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (e.KeyCode == Keys.Left) { linkLabel1.Left = linkLabel1.Left + 5; } return base.ProcessCmdKey(ref msg, keyData); }
Upvotes: 0
Reputation: 1711
When you want to move a Control, you have to reconfigure it's Control.Location property. So simply add or remove some dots form the Location.[X/Y].Property and that's all!
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
{
// have we space?
if(linkLabel1.Location.X >= 4)
// 5 dots to the left side
linkLabel1.Location = new Point(linkLabel1.Location.X - 5, linkLabel1.Location.Y);
}
}
EDIT: msdn
Location is a Point and a Point has a (x,y) - Coordinate.
Upvotes: 0
Reputation: 14672
I think its to do with the interception of events by the linklabel. With the link label present on the form the key down event will not be raised to the form.
Setting KeyPreview to true (on the form) goes someway towards fixing this. You should then get the event raised, though you might still have problems with the arrow keys.
Update:
Ok, this should work, add this:
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Left)
{
linkLabel1.Left = linkLabel1.Left + 5;
}
return base.ProcessDialogKey(keyData);
}
Upvotes: 0