Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7975837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:45:15+00:00 2026-06-04T08:45:15+00:00

When creating buttons dynamically, i wished to have a new MouseEventHandler. I did this

  • 0

When creating buttons dynamically, i wished to have a new MouseEventHandler. I did this like so,

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

However, as i needed to pass parameters to the event, i made generatePopup to accept an integer which would denote button number (so i could use further). In the past i would have just used an anonymous delegate to pass additional parameters across, but this doesn’t seem to work as i want to create a NEW instance, 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 on new MouseEventHandler. if i take this out, it works fine and i can pass additional data, but because it is not a new instance it doesn’t give each button its own event, like so;

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

does anyone know how to work around this? for example to use the anon delegate approach still, but still creating a new instance of the event?

EDIT: hmm, i tried this (parameters pass fine i believe) but it seems as though instead of creating a new mouseeventhandler for each button, it’s using the same one for all of the buttons, as i’ve set a label to test it’s getting the right button number, but all of the labels on the popup’s are returning the last button number instead of their correct one. any ideas?

EDIT 2: here’s my code block for generating 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’s my generatePopup method, which is the 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’s an image of what’s happening to, just for clarity:

example of error

Here we see both popup usercontrols given the label “2”, whereas i need each to have their correct value, “1” and “2”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:45:17+00:00Added an answer on June 4, 2026 at 8:45 am

    Try this:

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

    The new MouseEventHandler(...) part is optional – the compiler figures out the parameter types from the left side of the += assignment.

    EDIT The problem with your code is that it accesses modified closure. You should make a temp variable from i, and use it inside your lambda to fix the problem.

    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);
            };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating buttons dynamically ... for(int i=0; i<colSize;i++){ final Button btn = new Button(this);
Detailed scenario is like this : I am creating 10 buttons using a loop
I am creating buttons dynamically for a user-based app. Now I have to tell
I'm new to iPhone dev. My app is dynamically creating buttons, following from my
I have a problem with my project . I am creating link buttons dynamically
First off, I am not dynamically creating any controls. This is the order I
I'm creating buttons dynamically on an Excel userform with the following code: With Me.CurrentFrame.Controls.Add(Forms.CommandButton.1)
i have a problem in creating radio buttons dynamically, i have a text box
I have problem with handling dynamically created Buttons on Android. I'm creating N buttons
I'm creating buttons dynamically that have images as their background, The problem is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.