This is now working pretty well (just like I understood you). Hope it helps:
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 Feb09Exercise3
{
public partial class Form1 : Form
{
Random random = new Random();
int allButtons;
public Form1()
{
InitializeComponent();
foreach (Control c in Controls)
{
if (c is Button)
{
c.Click += new EventHandler(c_Click);
allButtons++;
}
}
RandomText();
}
private void c_Click(object sender, EventArgs e)
{
Button b = sender as Button;
string strText = b.Text;
if (strText != String.Empty)
{
string items = GetAdjacents(b.Name);
string[] values = items.Split(',');
string[] free = null;
int counter = 0;
for (int i = 0; i < values.Length; i++)
{
string name = "button" + values[i];
foreach (Control c in Controls)
{
if (c is Button)
{
if (c.Name == name && c.Text == String.Empty)
{
counter++;
Array.Resize(ref free, counter);
free[counter - 1] = name;
}
}
}
//FINAL DECISION OF THE NEW RE-LOCATION (using random selection between those places which are free (empty):
if ((i + 1) == values.Length)
{
string item = GetRandomReLocation(free);
//var _control = this.Controls.OfType<Button>().Where(c => c.Name == item);
(Controls[item] as Button).Text = strText;
b.Text = String.Empty;
}
}
}
}
private string GetRandomReLocation(string[] array)
{
string item = array[random.Next(0, array.Length)];
return item;
}
private void RandomText()
{
Button[] buttons = new Button[] { button1, button2, button3, button4, button5, button6,
button7, button8, button9, button10, button11, button12,
button13, button14, button15, button16 };
//insert into 12 buttons (out …