How to add a control to a panel in a form from another user control

I have form1.cs, and in this form I have panel1, in the load event of form1.cs I add a control to panel1. Now my problem is: I have an element called Numbers.cs, I need to add another control to this panel1, but from this control to the button event. How can i do this?

public partial class Number : UserControl
{
    public Number()
    {
        InitializeComponent();
    }

    private void btnAcceptWelcome_Click(object sender, EventArgs e)
    {
          //HERE I NEED TO PASS A CONTROL TO THE PANEL1 IN FORM1.CS
          //NOT SURE HOW TO DO THIS.
    }
}

ADDITIONAL INFORMATION

So basically I have a folder called UserControls, and in this folder I have

Numbers.cs
Letters.cs
Welcome.cs

All user controls, then I have a form

Form1.cs

Form1.cs Welcome Panel1 Form1.cs . Welcome.cs , , Numbers.cs. , Welcome.cs

+5
4

, Numbers Form1, Panel Control Collection.


, UserControl1

Form1

public partial class Form1 : Form
{
    UserControl2 mySecondControl = new UserControl2();
    public Form1()
    {
        InitializeComponent();
        userControl11.AddControl+=new EventHandler(SwapControls);

    }

    private void SwapControls(object sender, EventArgs e)
    {
        panel1.Controls.Remove(userControl11);
        userControl11.AddControl -= new EventHandler(SwapControls);
        panel1.Controls.Add(mySecondControl);
    }
}

UserControl

public partial class UserControl1 : UserControl
{
    public event EventHandler AddControl;
    public UserControl1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.AddControl(this, new EventArgs());
    }
}
+2

:

  • , Form1 ( ) Number

Number:

public partial class Number : UserControl
{
    //  event handler Form1 will subscribe to
    public EventHandler<EventArgs> OnWelcomeAccepted = (o, e) => { };

    public Number()
    {
        InitializeComponent();
    }

    private void btnAcceptWelcome_Click(object sender, EventArgs e)
    {
          //  raise the event
          OnWelcomeAccepted(sender, e);
    }
}

... 1 InitializeComponent(); ControlAdded:

public partial class Form1 : Form {
    public Form1()
    {
        InitializeComponent();

        this.ControlAdded += Control_Added;
        //  subscribe to the event and provide the implementation
        Number.OnWelcomAccepted += (o, e) => { Controls.Add(GetControl( )); }
    }

    private void Control_Added(object sender, System.Windows.Forms.ControlEventArgs e)
    {
        //  process size and placement and show
    }

}

- Form1. Form1 .

+2

, panel1 , form1, .

0

, , , . # , Button. . .

, , . , . :

public delegate void WelcomeClick(object sender, EventArgs e);

, , , :

public event WelcomeClick OnClick;

"Click Click" :

if (OnClick != null)
                OnClick(sender, e);
0

All Articles