這是你想要的東西的一種基本方法:
static string key = "Level solution";
char[] chars = key.ToCharArray();
void check (object sender)
{
var button = sender as Button;
character = Convert.ToChar(button.Text);
int i = 0;
foreach (char c in chars)
{
//checks every character to mark them
if (c == character)
{
chars[i] = ' ';
//Makes character unusable for later use
//Anything you want now for true letters, for example showing pics or adding them to a label
}
i++;
}
//checks every character of key again, to see if player is won
int count = 0;
foreach (char c in chars) {
if (c != ' ') count++;
//Adds a number for anything expect space
}
if (count == 0)
{
MessageBox.Show("You won!");
}
}
您可以將其粘貼在你的代碼的頂部,然後在任意按鈕僅使用此代碼:
check(sender);
我會扭轉行話:如何將相同的方法分配給幾個按鈕的事件處理程序。答:就像做一個,但始終使用相同的方法名稱。然後使用'sender'參數(轉換爲按鈕)來辨別觸發事件的按鈕。 – TaW