Assuming those little squares are colored buttons, I implemented the feature you mentioned. With little modification you could achieve the same.
![Here is an image of a simple form that I created to implement the feature asked](https://cdn.statically.io/img/i.sstatic.net/mLaidHvD.png)
The three red buttons are selectable and become green when clicked and can be moved with the arrow key when selected. There is no out of bound logic implementation, but it could easily be made. The code written to make it easily understood, so no size optimization was made.
Here is the code implementation for Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private List<Button> buttonList = new List<Button>();
private List<TextBox> textboxList = new List<TextBox>();
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
buttonList.Add(button1);
buttonList.Add(button2);
buttonList.Add(button3);
textBox1.Text = button1.Location.ToString();
textBox2.Text = button2.Location.ToString();
textBox3.Text = button3.Location.ToString();
textboxList.Add(textBox1);
textboxList.Add(textBox2);
textboxList.Add(textBox3);
buttonList.ForEach(button =>
{
button.PreviewKeyDown += new PreviewKeyDownEventHandler(button_PreviewKeyDown);
button.LocationChanged += new EventHandler(OnButtonLocationChange);
});
this.KeyDown += new KeyEventHandler(Form1_KeyPress);
}
private void button1_Click(object sender, EventArgs e)
{
button_logic(sender, e);
}
private void OnButtonLocationChange(object sender, EventArgs e)
{
textboxList[buttonList.IndexOf((Button)sender)].Text = ((Button)sender).Location.ToString();
}
private void Form1_KeyPress(object sender, KeyEventArgs e)
{
if (buttonList.FirstOrDefault(x => x.BackColor == Color.Green) == null)
return;
if (e.KeyCode == Keys.Up)
{
Button tempButton = buttonList.First(x => x.BackColor == Color.Green);
buttonList.First(x => x.BackColor == Color.Green).Location = new Point(tempButton.Location.X, tempButton.Location.Y - 10);
}
else if (e.KeyCode == Keys.Down)
{
Button tempButton = buttonList.First(x => x.BackColor == Color.Green);
buttonList.First(x => x.BackColor == Color.Green).Location = new Point(tempButton.Location.X, tempButton.Location.Y + 10);
}
else if (e.KeyCode == Keys.Left)
{
Button tempButton = buttonList.First(x => x.BackColor == Color.Green);
buttonList.First(x => x.BackColor == Color.Green).Location = new Point(tempButton.Location.X - 10, tempButton.Location.Y );
}
else if (e.KeyCode == Keys.Right)
{
Button tempButton = buttonList.First(x => x.BackColor == Color.Green);
buttonList.First(x => x.BackColor == Color.Green).Location = new Point(tempButton.Location.X + 10, tempButton.Location.Y);
}
}
private void button_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
var keys = new[] { Keys.Left, Keys.Right, Keys.Up, Keys.Down };
if (keys.Contains(e.KeyData))
e.IsInputKey = true;
}
private void button2_Click(object sender, EventArgs e)
{
button_logic(sender, e);
this.Focus();
}
private void button3_Click(object sender, EventArgs e)
{
button_logic(sender, e);
}
private void button_logic(object sender, EventArgs e)
{
foreach(Button button in buttonList)
{
if((Button)sender == button)
{
button.BackColor = Color.Green;
}
else
{
button.BackColor = Color.Red;
}
}
this.Focus();
}
}
}