简体   繁体   English

如何将控件添加到另一个用户控件的窗体上的面板

[英]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. 我有一个form1.cs,在那种形式我有一个panel1,在form1.cs的load事件中我向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. 现在我的问题是,我有一个名为Numbers.cs的控件,我需要向该panel1添加另一个控件,但是在按钮事件中从该控件添加。 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 所以基本上我有一个名为UserControls的文件夹,我在那个文件夹中

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. Form1.cs实例化欢迎,并在表单加载时将其添加到Form1.cs上的Panel1中。 Then Welcome.cs has a button, when I click this button I need to swap to Numbers.cs. 然后Welcome.cs有一个按钮,当我单击此按钮时,我需要交换到Numbers.cs。 But I dont know how to do this from Welcome.cs 但我不知道如何从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. 另一种方法是使用Numbers引发的自定义事件并由Form1处理以传递控件并将其添加到Panel的Control Collection中。


This is an example of an Custom Event added to UserControl1 这是添加到UserControl1的自定义事件的示例

Form1 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 假设Form1具有(或可以获得)对Number的引用

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

...Form1 will have a subscription after InitializeComponent() ; ... Form1将 InitializeComponent() 之后订阅; note the additional subscription to ControlAdded : 请注意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 . 没有其他控件应该直接向Form1添加任何内容。 Let Form1 control it's children. 让Form1控制它的孩子。

一种方法是在数字中引用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. 由于在C#对象中总是通过引用,因此您将对Button事件中的同一控件执行操作。 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: 在Welcome中你需要创建事件处理程序,它可以是静态的,也可以是实例的一部分:

public event WelcomeClick OnClick;

Inside the Button Click event in welcome you can just call that event with the same parameters: 在Welcome中的Button Click事件中,您可以使用相同的参数调用该事件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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