简体   繁体   中英

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. 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. 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. 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. However, when I click the button that exists on buttonPanel.cs, I can't get form1 mainPanel to change it's content.

ie: WelcomeMenu.cs has a button called btnPage2 that should change mainPanel controls to show Page2.cs usercontrol instead of Welcome.cs usercontrol.

I want to be able to use in the button click handler on 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.

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. 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.

Update: 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'

UPDATE 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. I have 2 panels on Form1. menuPanel and mainPanel. They are both set to Modifiers = Public.

Here is my 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:

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. 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:

        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:

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

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。

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