Access Controls (Text Boxes and Combo Boxes) added in Runtime to Windows Foam

63 Views Asked by At

I am adding User Control to my Main Windows Foam, the User Control consist of Tablelayoutpanel which include 2 Combo Boxes,1 Button and 4 Text Boxes, as shown in the picture below:enter image description here

How can I access each of these controls as I add more Controls in Runtime Such as this in image belowenter image description here

In my practice I know to access controls which are created in Design Time but I don't know how to access Control which are generated in Runtime.

I tried Button Click event, but It shows Click event of Single button only, how can I access Click Event for multiple button added in Runtime.

1

There are 1 best solutions below

0
Jiale Xue - MSFT On BEST ANSWER

Update2:

Do you mean remove the control's parent control?

private void button1_Click_1(object sender, System.EventArgs e)
{
    UserControl1 userControl11 = new UserControl1();
    tableLayoutPanel1.Controls.Add(userControl11);
    foreach (Control item in userControl11.Controls)
    {
        if (item.Name == "button1")
        {
            item.Click += (sender1, e1) =>
            {
                Control childControl = sender1 as Control;
                if (childControl != null && childControl.Parent != null)
                {
                    Control parentControl = childControl.Parent;
                    tableLayoutPanel1.Controls.Remove(parentControl);
                }
            };
        }
    }
}

enter image description here


Update:

I misunderstood your runtime creation.

In fact, even multiple usercontrols are created at design time. They all have their own names.

You can add events to them using the same method.

private void button1_Click(object sender, EventArgs e)
{
    foreach (Control ctrl in this.userControl12.Controls)
    {
        if (ctrl.Name is "button1")
        {
            Button btn = (Button)ctrl;
            btn.Click += new EventHandler(Button1_Click);
        }
        else if (ctrl.Name is "button2")
        {
            Button btn = (Button)ctrl;
            btn.Click += new EventHandler(Button2_Click);
        }
    }
}
private void Button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("Hello");
}
private void Button2_Click(object sender, EventArgs e)
{
    MessageBox.Show("World");
}

enter image description here


This example shows how to add your own events to multiple buttons contained in a usercontrol.

Create a usercontrol as follows:

enter image description here

Create a winform project as follows:

using System;
using System.Drawing;
using System.Windows.Forms;
using WindowsFormsControlLibrary1;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UserControl1 userControl = new UserControl1();
            foreach (Control ctrl in userControl.Controls)
            {
                if (ctrl.Text is "button1")
                {
                    Button btn = (Button)ctrl;
                    btn.Click += new EventHandler(Button1_Click);
                }
                else if (ctrl.Text is "button2")
                {
                    Button btn = (Button)ctrl;
                    btn.Click += new EventHandler(Button2_Click);
                }

            }
            userControl.Location = new Point(0, 0);
            // userControl.Size = new Size(200, 100);
            panel1.Controls.Add(userControl);

        }
        private void Button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello");
        }
        private void Button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("World");
        }
    }
}

enter image description here


Using this as an example, you can add individual events to them, or you can add a unified event.

Just find them from the corresponding container.