Creating a delegate for a new instance of MouseEventHandler

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)
    {
      //  DBConnector mDBConnector = new DBConnector();
      //  DataTable dataTable = mDBConnector.Select("SELECT * FROM devices WHERE deviceID = " + 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:

example of error

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".

+3
7

:

this.Controls[btnAdd.Name].MouseClick += new MouseEventHandler(
    (sender, evt) => {
        generalMethods.generatePopup()
    }
);

new MouseEventHandler(...) - +=.

EDIT , . i , .

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);
        var temp = i;
        this.Controls[btnAdd.Name].MouseClick += (sender, e) =>
            {
            int index = temp;
            generalMethods.generatePopup(sender, e, index);
        };
}
+2

generatePopup? MouseEventHandler, ( ). - :

btnAdd.MouseClick += generalMethods.generatePopup();

/* in generalMethods */
private static int ButtonIndex = 0;
public MouseEventHandler generatePopup()
{
   int tempIndex = ButtonIndex++;
   return new MouseEventHandler((sender, ea) => generalMethods.generatePopup(sender, ea, tempIndex));
}

, , , generatePopup MouseEventHandler . , , , tempIndex, . , , ( tempIndex) generatePopup ( tempIndex). , , , .

.

: , . - , (IE: , - )? , :

(sender, ea) => generalMethods.generatePopup(sender, ea, (int)((Control)sender).Tag)

, , . ( ), , MouseEventHandler, . Loop Lambdas , .

+1

, , . mouseclick .

0

. .

this.Controls[btnAdd.Name].MouseClick += new MouseEventHandler(MyControl_MouseClick);

//...

void MyControl_MouseClick(object sender, MouseEventArgs e)
{
    var button = sender as Button;

    if (button.Name == "Add") // here is how to retrieve the button name
    {
        ...
    }
}
0

.

, . .

:

  // Define a delegate named LogHandler, which will encapsulate
    // any method that takes a string as the parameter and returns no value
       public delegate void LogHandler(string message);

    // Define an Event based on the above Delegate
       public event LogHandler Log;

Ref this .

: Winforms - 1 Windows Forms .NET Framework :

public partial class UserControl1 : TextBox
0

:

this.Controls[btnAdd.Name].MouseClick += 
  (sender, e) => generalMethods.generatePopup(sender, e, i);

, .

0

You will need to declare the index in the inner scope, because otherwise it will refer to your loop parameter.

for (int i = 0; i < 42; i++)
{
    this.Controls[name].MouseClick += (sender, e) =>
    {
        int index = i;
        generalMethods.generatePopup(sender, e, index);
    }
}

You do not need an assigned handler for each button (one for all will work fine), but you will have to provide each button with its own index object.

0
source

All Articles