views:

314

answers:

2

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  A: 

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.

Aaronaught
A: 

So...

Funny thing. Apparently one of those parent controls was disabled. Fancy that.

I re-created about half of the functionality on a different set of controls and got it working. By then I realized the problem had to be related to one of the controls, and not the logic.

I have no idea when, how, or why that control got disabled, but that certainly goes to the top of my 'stupid things to check' list. >_<

Tinkerer_CardTracker