0

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

}

  • UserControl's have a property called `ParentForm`. Try using that in place of `Form1`, which is an inappropriate reference. Cast it to Form1. Not the best setup though. Your UserControl really should just raise an event and let the form take care of this business. Also, Clearing controls does not Dispose of them. You have to Dispose of them when you are done. – LarsTech Dec 04 '15 at 18:49
  • @LarsTech is correct. I missed that part. However, the mainPanel still neds to be set to Public for it's Modifier for other UserControls to be able to reference it. – The Sharp Ninja Dec 04 '15 at 18:52
  • That sounds like what I need. Not sure how to phrase my search question. Can you tell me what I should search to find some examples of how to let the event handle this type of issue? – Darth Vader's Son Dec 04 '15 at 18:58
  • See [How do I make an Event in the Usercontrol and Have it Handeled in the Main Form?](http://stackoverflow.com/q/7880850/719186) – LarsTech Dec 04 '15 at 19:01
  • Also, I tried replacing Form1 with ParentForm and now I get an error on mainPanel that states: "'Form' does not contain a definition for 'mainPanel' and no extension method 'mainPanel' accepting a first argument of type 'Form' could be found ( are you missing a using directive or an assembly reference?)" – Darth Vader's Son Dec 04 '15 at 19:01
  • You missed the part where I said to cast it to Form1. – LarsTech Dec 04 '15 at 19:04
  • I'm not sure how to do that. I searched and found a foreach example that I don't understand. foreach(UserControl uc in plhMediaBuys.Controls) { MyControl c = uc as MyControl; if (c != null) { c.PublicPropertyIWantAccessTo; } } – Darth Vader's Son Dec 04 '15 at 20:13
  • `((Form1)this.ParentForm).mainPanel.Controls.Add(frm);` – LarsTech Dec 04 '15 at 20:16

2 Answers2

1

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

Set the accessibility Modifiers property of mainPanel from private to internal public.

The Sharp Ninja
  • 1,041
  • 9
  • 18
  • I apologize for my denseness. I can't find any property called accessibility that isn't related to accessibility applications. I did find a property under Design called Modifiers that is currently listed as Private. I changed that to Internal and it still doesn't work. – Darth Vader's Son Dec 04 '15 at 18:33
  • Ok. I changed Modifiers to Public and I get the error I listed above when I try to add the control. – Darth Vader's Son Dec 04 '15 at 18:50