2016-11-16 66 views
0

構建一個程序,在按下按鈕時,將按下的事件保存爲數組中的「1」。我想擁有3個按鈕,因此陣列在指定的時間長度內會有3個滿了0或1的字段(按下或不按下)。不太熟悉C#,所以我不知道從哪裏開始。有任何想法嗎?尋找一種在按下按鈕時獲取按鈕ID的方法

+0

開始與代表和事件,你會得到這個想法。 https://msdn.microsoft.com/en-us/library/orm-9780596521066-01-17.aspx – Zinov

回答

0

首先,您應該熟悉C#語言。開始的一個好方法:https://www.tutorialspoint.com/csharp/

我不知道你的最終目的是什麼,但如果你真的想要去與陣列然後嘗試這樣的事:

// declare it as class member 
int[] buttonStates = new int[3] {0, 0, 0}; // immediate initialization 

有幾種方法來處理按鈕按下。它取決於你正在開發的平臺:WinForms,ASP.NET,WPF等?

但一般來說:
你的按鈕事件處理程序應該包含將數組1放入的代碼。

// in the first button handler: 
buttonStates[0] = 1; 

// in the second button handler: 
buttonStates[1] = 1; 

// in the third button handler: 
buttonStates[2] = 1; 
0

添加到提供的答案和您的標題建議獲得按鈕ID。要唯一標識每個按鈕,可以使用按鈕的「名稱」(要識別的元素的唯一名稱)或「標記」(可用於存儲有關元素的自定義信息)屬性。 您可以設置按鈕ID按鈕的標籤屬性:

Button btn = new Button(); 
btn.Tag = 1; 

然後在事件處理

private void Btn_Click(Object sender, RoutedEventArgs e) 
{ 
Button b = (Button)sender; 
int btnTag = (int)b.Tag; //check the Id 
} 

可以參考該按鈕使用這種「ID」