简体   繁体   English

通过单击动态添加的用户控件上的按钮来添加/删除面板控件

[英]Add/Remove panel controls by clicking button on dynamically added usercontrol

I am new to c# windows forms and I'm trying to make a GUI that has two panels. 我是C#Windows窗体的新手,我正在尝试制作一个具有两个面板的GUI。 The left panel is used to display dynamically added user controls that contain buttons. 左侧面板用于显示包含按钮的动态添加的用户控件。 The right panel is used to display dynamically added usercontrols that contain other controls: textboxes, labels, comboboxes, buttons, etc. 右侧面板用于显示动态添加的用户控件,其中包含其他控件:文本框,标签,组合框,按钮等。

I've created a form1 that has the two panels. 我创建了一个具有两个面板的form1。 I can successfully load both panels with different UserControl.cs content by using a menu that I've added to the top of the form. 通过使用添加到表单顶部的菜单,我可以成功加载具有不同UserControl.cs内容的两个面板。 When I click a menu option, form1 buttonPanel is loaded with the appropriate buttonPanel.cs content and form1 mainPanel is loaded with the appropriate mainPanel.cs content. 当我单击菜单选项时,向form1 buttonPanel加载适当的buttonPanel.cs内容,向form1 mainPanel加载适当的mainPanel.cs内容。 However, when I click the button that exists on buttonPanel.cs, I can't get form1 mainPanel to change it's content. 但是,当我单击buttonPanel.cs上存在的按钮时,无法获得form1 mainPanel来更改其内容。

ie: WelcomeMenu.cs has a button called btnPage2 that should change mainPanel controls to show Page2.cs usercontrol instead of Welcome.cs usercontrol. 即:WelcomeMenu.cs有一个名为btnPage2的按钮,该按钮应更改mainPanel控件以显示Page2.cs用户控件,而不是Welcome.cs用户控件。

I want to be able to use in the button click handler on UserControl.cs: 我希望能够在UserControl.cs的按钮单击处理程序中使用:

mainPanel.Controls.Clear();
UserControl usrCtl = new UserControl();
Form1.mainPanel.Controls.Add(usrCtl);

My problem seems to be that WelcomeMenu.cs cannot see or access Form1 mainPanel. 我的问题似乎是WelcomeMenu.cs无法查看或访问Form1 mainPanel。

Is there a way to make this work? 有没有办法使这项工作? Or am I trying to do this the wrong way? 还是我尝试以错误的方式执行此操作?

My reason for this method is so I can load a new buttonPanel.cs usercontrol and mainPanel.cs usercontrol for each department and then be able to change mainPanel content for each button I click in the current buttonPanel. 我采用这种方法的原因是,我可以为每个部门加载新的buttonPanel.cs usercontrol和mainPanel.cs usercontrol,然后能够更改在当前buttonPanel中单击的每个按钮的mainPanel内容。 I'm trying to avoid creating a bunch of panels on Form1 and then hiding them and only making them visible when I need them. 我试图避免在Form1上创建一堆面板,然后隐藏它们,只在需要它们时才显示它们。

Update: buttonMenu.cs 更新:buttonMenu.cs

{ {

{
 public partial class ucWelcomeMenu : UserControl

    public ucWelcomeMenu()
    {
        InitializeComponent();
    }

    private void btnPage2_Click(object sender, EventArgs e)
    {
        Form1.mainPanel.Controls.Clear();
        ucWelcome frm = new ucWelcome();
        Form1.mainPanel.Controls.Add(frm);
    }
}

} }

Form1.mainPanel.Controls.Add(frm) generates an error on Form1.mainPanel that states: "An object reference is required for the non-static field, method, or property 'Form1.mainPanel' Form1.mainPanel.Controls.Add(frm)在Form1.mainPanel上生成错误,指出:“非静态字段,方法或属性'Form1.mainPanel'需要对象引用。

UPDATE 2: 更新2:

Ok. 好。 I've searched several different links and found some helpful information. 我搜索了几个不同的链接,并找到了一些有用的信息。 However, I am still unable to fire an event from a button click in a dynamically added UserControl.cs. 但是,我仍然无法通过动态添加的UserControl.cs中的按钮单击来触发事件。 I have 2 panels on Form1. 我在Form1上有2个面板。 menuPanel and mainPanel. menuPanel和mainPanel。 They are both set to Modifiers = Public. 它们都设置为Modifiers = Public。

Here is my Form1: 这是我的Form1:

namespace TestUserControl

public partial class Form1 : Form
{
    private ucWelcomeMenu welc = new ucWelcomeMenu();

    public Form1()
    {
        InitializeComponent();

        ucWelcomeMenu welcomeMenu = new ucWelcomeMenu();
        menuPanel.Controls.Add(welcomeMenu);

        welc.ButtonClick += new EventHandler(this.CustomEvent_Handler);
    }

    private void CustomEvent_Handler(object sender, EventArgs e)
    {
        MessageBox.Show("Yes");
    }
}

} }

And Here is my UserControl: 这是我的UserControl:

namespace TestUserControl.UserControls

public partial class ucWelcomeMenu : UserControl
{
    public event EventHandler ButtonClick;

    public ucWelcomeMenu()
    {
        InitializeComponent();
    }

    private void btnPage2_Click(object sender, EventArgs e)
    {
        if (ButtonClick != null)
            ButtonClick(sender, e);
    }
}

} }

Ok. 好。 I am definitely a little slow. 我肯定有点慢。 I found my problem. 我发现了问题。 I was Instantiating the ucWelcomeMenu twice. 我两次实例化了ucWelcomeMenu。 Once I removed the private instantiation above the constructor, the event fired just fine. 一旦删除了构造函数上方的私有实例,事件就可以正常触发了。 Thanks for all the input. 感谢所有的投入。 It sent me to some good links with some very helpful information. 它使我获得了一些很好的链接,其中包含一些非常有用的信息。

Here is what I ended up with: 我最终得到的是:

Form1 Menu Option Click Handler: Form1菜单选项单击处理程序:

        private void option1ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        UserControl1 ctl1 = new UserControl1();
        menuPanel.Controls.Add(ctl1);

        ctl1.btn1Click += new EventHandler(btn1_Click);

        UserControl2 ctl2 = new UserControl2();
        mainPanel.Controls.Add(ctl2);
    }

Button 1 Click Handler on Form1: 按钮1单击Form1上的Handler:

        private void btn1_Click(object sender, EventArgs e)
    {
        mainPanel.Controls.Clear();
        UserControl2 frm = new UserControl2();
        mainPanel.Controls.Add(frm);
    }

UserControl1.cs UserControl1.cs

    public partial class UserControl1 : UserControl
{
    public event EventHandler btn1Click;

    public UserControl1()
    {
        InitializeComponent();
    }

    private void btn1_Click(object sender, EventArgs e)
    {
        if (btn1Click!= null)
            btn1Click(sender, e);
    }
}

将mainPanel的 可访问性 Modifiers属性设置为private到 内部 public。

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

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