2010年3月24日 星期三

[C#] 動態產生物件 RadioButton 後,自訂事件

private void button2_Click_1(object sender, EventArgs e)

{
string[] KK = new string[3];
KK[0] = "Yes";
KK[1] = "No";
KK[2] = "Maybe";
System.Windows.Forms.RadioButton[] radioButtons = new System.Windows.Forms.RadioButton[3];
for (int i = 0; i < 3; ++i)
{
radioButtons[i] = new RadioButton();
radioButtons[i].Text = KK[i];
radioButtons[i].Location = new System.Drawing.Point(10, 10 + i * 20);
this.Controls.Add(radioButtons[i]);
}
radioButtons[0].Click += new System.EventHandler(this.OhYa_Click1);
radioButtons[1].Click += new System.EventHandler(this.OhYa_Click2);
radioButtons[2].Click += new System.EventHandler(this.OhYa_Click3);
}

private void OhYa_Click1(object sender, EventArgs e)
{
MessageBox.Show("1");
}

private void OhYa_Click2(object sender, EventArgs e)
{
MessageBox.Show("2");
}

private void OhYa_Click3(object sender, EventArgs e)
{
MessageBox.Show("3");
}

沒有留言:

張貼留言