Nested dynamic controls using custom event handlers

I am creating WinForm with quite a few dynamic elements, and I think that I am having problems with the parent / child process relationships within the nested controls. All existing questions that I could find were exclusive to WebForms, which was not entirely useful.

I also had problems with user controls, but this could be due to a problem.

I am trying to display several PictureBoxes, each of which has a number of related NUDs. At first I did this by doing a lot of controls manually, but now I want to automate the process and reuse the code elsewhere.

The actual code is a bit more complicated than that, but here the important bits are in the combination of PseudoCode and the 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");
}

Hope this was clear, just in case that was important, here are a few more stories.

I have a very large FlowLayoutPanel that contains some child panels. One of those kids panels is the area I'm working on right now. (e.g. panel_book panel above) this panel is what I dynamically add child panels with PictureBox and friends.

It is annoying that those MouseEnter and Click events do not fire. Generally. I used to add event handlers at runtime when the controls were not dynamic and had never experienced such big problems. I'm sure I even did this with nested controls.

, , . , , , ?

, :)

+3
2

, ", ", , (.. , , ); , , .

( , Windows):

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");
}

PictureBox, . , , .

, , , /.

+2

...

. -, . , .

. , , .

, , , , , , " , ". > _ & ;

0

Source: https://habr.com/ru/post/1735534/


All Articles