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, :)