2012-05-11 33 views
0

我有一個基於我的數據庫創建控制面板的類。它創建一個面板,每個面板上都有一個按鈕,每個DB都有一個按鈕。我如何解決一個特定的按鈕來進行點擊事件?爲clickevent生成一個生成的按鈕

我是一個新手,也許在我的頭上升技,但你不學會在淺水中游泳;) 任何幫助表示讚賞!

while (myDataReader.Read()) 
{ 
    i++; 
    Oppdrag p1 = new Oppdrag(); 
    p1.Location = new Point (0, (i++) * 65); 
    oppdragPanel.Controls.Add(p1); 
    p1.makePanel(); 
} 

class Oppdrag : Panel 
{ 
    Button infoBtn = new Button(); 

    public void makePanel() 
    { 
    this.BackColor = Color.White; 
    this.Height = 60; 
    this.Dock = DockStyle.Top; 
    this.Location = new Point(0, (iTeller) * 45); 

    infoBtn.Location = new Point(860, 27); 
    infoBtn.Name = "infoBtn"; 
    infoBtn.Size = new Size(139, 23); 
    infoBtn.TabIndex = 18; 
    infoBtn.Text = "Edit"; 
    infoBtn.UseVisualStyleBackColor = true; 
    } 
} 

回答

1

您需要一個方法來匹配通過單擊按鈕拋出的事件。

即)

void Button_Click(object sender, EventArgs e) 
{ 

    // Do whatever on the event 
} 

然後你需要點擊事件分配的方法。

p1.infoBtn.Click += new System.EventHandler(Button_Click); 

希望這會有所幫助。

+0

甜! Thx,正是我需要的! – MrHaga

1

您可以在創建按鈕時添加按鈕的事件處理程序。您甚至可以爲每個按鈕添加一個獨特的CommandArgument,以便您可以將一個按鈕與另一個按鈕區分開來。

public void makePanel() 
{ 
    /* ... */ 
    infoBtn.UseVisualStyleBackColor = true; 
    infoBtn.Click += new EventHandler(ButtonClick); 
    infoBtn.CommandArgument = "xxxxxxx"; // optional 
} 

public void ButtonClick(object sender, EventArgs e) 
{ 
    Button button = (Button)sender; 
    string argument = button.CommandArgument; // optional 
}