When creating buttons dynamically, I wanted to have a new MouseEventHandler. I made it so
this.Controls[btnAdd.Name].MouseClick += new MouseEventHandler(generalMethods.generatePopup());
However, since I needed to pass parameters to the event, I did generatePopup to accept an integer that would represent the button number (so that I can use further). I used to just use an anonymous delegate to pass additional parameters, but this does not seem to work, since I want to create an instance of NEW, as such;
this.Controls[btnAdd.Name].MouseClick += delegate(object sender, MouseEventArgs e) { new MouseEventHandler( generalMethods.generatePopup(sender, e, i); };
it seems that the error is pointed at the new MouseEventHandler. if I do, it works fine, and I can transfer additional data, but since it is not a new instance, it does not give each button its own event, for example:
this.Controls[btnAdd.Name].MouseClick += new MouseEventHandler(generalMethods.generatePopup());
Does anyone know how to get around this? for example, use the anonymous delegate approach, but still create a new instance of the event?
EDIT: hmm, I tried this (the parameters go fine, I believe), but it seems that instead of creating a new mouseeventhandler for each button, it uses the same for all buttons, since I set the label to check, getting the right button number, but all the popup shortcuts return the last button number, not the correct one. any ideas?
EDIT 2: here is my code block for creating buttons:
for (int i = 0; i <= count && i < 2; i++)
{
Button btnAdd = new Button();
btnAdd.Text = dataTable.Rows[i]["deviceDescription"].ToString();
btnAdd.Location = new Point(x, y);
btnAdd.Tag = i;
btnAdd.Name = "btn" + i.ToString();
btnAdd.BackColor = Color.Green;
this.Controls.Add(btnAdd);
this.Controls[btnAdd.Name].MouseClick += (sender, e) =>
{
int index = i;
generalMethods.generatePopup(sender, e, index);
};
and here is my generatePopup method, which is mouseEventHandler:
public void generatePopup(object sender, MouseEventArgs e, int buttonNumber)
{
DeviceBreakdownPopup popupDevice = new DeviceBreakdownPopup();
popupDevice.lblDeviceNo.Text = buttonNumber.ToString();
PopupWindow popup = new PopupWindow(popupDevice);
popup.Show(Cursor.Position);
}
here is an image of what is happening, just for clarity:

Here we see both pop-up user controls with a label of "2", while each of them must have the correct value of "1" and "2".