2012-05-11 5 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 
} 
관련 문제