المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Adding an event to a dynamically created Button



C# Programming
05-30-2009, 10:10 PM
Hello I am trying to add an event to a button that I created dynamically, but I can not get the event to trigger.

I also want to reference the button in an array of buttons, but I have been unsuccessfull.

Any Ideas? Thank you in advance.


private void btnDouglasBrowse_Click(object sender, EventArgs e)
{
if (fbdDouglasMain.ShowDialog() == DialogResult.OK)
{
tbDouglasFolder.Text = fbdDouglasMain.SelectedPath;
btnDouglasBrowse.Enabled = false;
jtnDouglasMain.FolderName = tbDouglasFolder.Text;
for (int i = 0; i < 20; i++)
{
Button buttonName = new Button();
//buttonName.********.X = i * 20;
buttonName.******** = new Point(i * 20 + 20,530);
buttonName.Width = 20;
buttonName.Text = i.ToString();
buttonName.DoubleClick += new EventHandler(buttonName_DoubleClick); // This line
this.Controls.Add(buttonName);
}

}

}

private void buttonName_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show("The calculations are complete", "My Application",MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);


}