Не работает движение кнопки - C#
Формулировка задачи:
Кто подскажет почему не работает движение кнопки?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.KeyDown += new KeyEventHandler(onKeyDown);
}
public void onKeyDown (object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
this.button1.Location = new Point(button1.Location.X - 10, button1.Location.Y);
if (e.KeyCode == Keys.Right)
this.button1.Location = new Point(button1.Location.X + 10, button1.Location.Y);
Refresh();
}
}
}Решение задачи: «Не работает движение кнопки»
textual
Листинг программы
public MainForm()
{
InitializeComponent();
this.KeyPreview = true;
}
public void MainForm_KeyDown (object sender, KeyEventArgs e)
{
e.Handled = true; //если установить false или убрать, то клавиша еще и обработается контролом, на котором фокус находится, то есть дважды - формой и контролом
if (e.KeyCode == Keys.Left)
this.button1.Location = new Point(button1.Location.X - 10, button1.Location.Y);
if (e.KeyCode == Keys.Right)
this.button1.Location = new Point(button1.Location.X + 10, button1.Location.Y);
Refresh();
}