2012-05-11 11 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

Sweet! 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 
} 
関連する問題