Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

windows

I have a form1.cs and in that form I have a panel1, in the load event of the form1.cs I am adding a control to the panel1. Now my issue is, I have a control called Numbers.cs, I need to add another control to that panel1 but from this control in a 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.
    }
}

MORE INFO

So basically I have a folder called UserControls and in that folder I have

Numbers.cs
Letters.cs
Welcome.cs

All of them user controls, then i have a form

Form1.cs

Form1.cs instantiates Welcome and it is added to a Panel1 on the Form1.cs on form load. Then Welcome.cs has a button, when I click this button I need to swap to Numbers.cs. But I dont know how to do this from Welcome.cs

like image 251
user710502 Avatar asked Oct 08 '22 07:10

user710502


2 Answers

Another way would be to use a Custom Event raised by Numbers and handled by Form1 to pass the control and add it to your Panel's Control Collection.


This is an example of an Custom Event added to 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());
    }
}
like image 150
Mark Hall Avatar answered Oct 12 '22 11:10

Mark Hall


Note:

  1. Untested code
  2. Assuming Form1 has (or can get) a reference to Number

Add an event handler to 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);
    }
}

...Form1 will have a subscription after InitializeComponent(); note the additional subscription to 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
    }

}

No other control should be adding anything directly to Form1. Let Form1 control it's children.

like image 43
IAbstract Avatar answered Oct 12 '22 12:10

IAbstract