简体   繁体   中英

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

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

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

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.

一种方法是在数字中引用panel1,因为它们都是在form1中创建的,你可以将一个作为参数传递给另一个构造函数。

It's not very clear from your description but you can just pass the control you want in the constructor or a property. Since in C# objects are always by reference you will be action on the same control in the Button event. You can always write your own event and have the panel register for it. A more complete code sample would be great.

I'm still a little unsure of what you're doing exactly but that's OK. I think the most flexible approach is to create your own custom event. Outside of any class create a delegate:

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

Inside Welcome you need to create the event handler, it can be either static or part of the instance:

public event WelcomeClick OnClick;

Inside the Button Click event in welcome you can just call that event with the same parameters:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM