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 646151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:34:50+00:00 2026-05-13T21:34:50+00:00

I’m building a WinForm with quite a few dynamic elements, and I think I’m

  • 0

I’m building a WinForm with quite a few dynamic elements, and I think I’m having some trouble with the parent/child relationship within nested controls.
All the existing questions I could find seemed exclusive to WebForms, which wasn’t entirely useful.

I’ve also had some trouble with custom-made controls, but that may be a related issue.

I’m trying to display a number of PictureBoxes, each with a number of associated NUDs. I initially did this by making lots of controls by hand, but now I want to automate the process and re-use the code elsewhere.

The actual code is a bit more complicated than this, but here’s the important bits in a mix of PseudoCode and actual code

panel_book.Controls.Clear();

for (loop controls)
{

    //INITIALIZE CHILD CONTROLS
    PictureBox tempBox = new PictureBox();
    NumericUpDown t1 = new NumericUpDown();
    NumericUpDown t2 = new NumericUpDown();
    NumericUpDown t3 = new NumericUpDown();
    NumericUpDown t4 = new NumericUpDown();

    tempBox.Image = getImage();
    tempBox.Size = tempBox.Image.Size;
    tempBox.Tag = getValue();



        //THIS IS WHAT IS GIVING ME TROUBLE
        //=======================================================
    tempBox.MouseEnter += new EventHandler(Binder_MouseEnter);
    tempBox.Click += new EventHandler(smallCardNew_Click);



        //THINGS I'VE TRIED
    tempBox.BringToFront();
    tempBox.Focus();


    t1.Size = new Size();
    t2.Size = t1.Size; t3.Size = t1.Size; t4.Size = t1.Size;
    t1.Location = new Point();
    t2.Location = new Point(); t3.Location = new Point(); t4.Location = new Point();
    t1.Value = 0;
    t2.Value = 0; t3.Value = 0; t4.Value = 0; 
        t1.Enabled = true; t2.Enabled = true;
    t3.Visible = false; t4.Visible = false;


    //CREATE THE NEW PARENT CONTROL (PANEL)
    Panel tempPanel = new Panel();
    tempPanel.Margin = new Padding(0, 0, 0, 0);
    tempPanel.Controls.Add(tempBox);
    tempPanel.Controls.Add(t1);
    tempPanel.Controls.Add(t2);
    tempPanel.Controls.Add(t3);
    tempPanel.Controls.Add(t4);

    tempPanel.Size = new Size();
    tempPanel.Location = new Point();

    panel_book.Controls.Add(tempPanel);


}//end loop


///

void smallCardNew_Click(object sender, EventArgs e)
{
    MessageBox.Show("Click Event Triggered");
}
void Binder_MouseEnter(object sender, EventArgs e)
{
    MessageBox.Show("Mouse Enter Event Triggered");
}

Hopefully that was clear, just in case its important, here’s some more background.

I have a very large FlowLayoutPanel which contains some child panels.
One of those child panels is the area I’m working on now. (called panel_book above)
that panel is what I’m dynamically adding child panels with the PictureBox & friends to.

The annoying thing is, those MouseEnter and Click events aren’t getting triggered. At all.
I’ve added event handlers at runtime before, when the controls weren’t dynamic, and never had this much trouble. I’m pretty sure I even did this with nested controls as well.

Lastly, I’ve considered making that last child panel into its own custom Control, but have had similar issues. Presumably finding the fix to this problem will fix that as well, but if you know it won’t, could you please point me in the right direction?

Thanks,
🙂

  • 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-05-13T21:34:50+00:00Added an answer on May 13, 2026 at 9:34 pm

    I don’t see anything obviously wrong with the code aside from the list of “things I’ve tried” being invalid until the control actually has a handle (i.e. is added to a parent and all ancestors also have parents, going up to the form level); however, you need to work through this methodically, start from the smallest reproducible case instead of a big blob of code.

    The following code does work (to test, just drop a button and flow panel on a Windows form):

    private void button1_Click(object sender, EventArgs e)
    {
        PictureBox pb = new PictureBox();
        pb.Location = new Point(0, 0);
        pb.Size = new Size(300, 300);
        pb.Image = SomeImage;
        pb.Click += new EventHandler(PictureBoxClick);
    
        Panel panel = new Panel();
        panel.Location = new Point(10, 40);
        panel.Size = new Size(300, 300);
        panel.Controls.Add(pb);
    
        flowLayoutPanel1.Controls.Add(panel);
    }
    
    private void PictureBoxClick(object sender, EventArgs e)
    {
        MessageBox.Show("Picture box clicked");
    }
    

    If I execute this code and click on the PictureBox, I get the message box showing up. Start from here, start making this look more like your real code, and eventually you will hit upon the problem.

    Whatever the problem is, it’s not just in the code you posted, it’s in the interaction with other code/designer elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.