0

I have a Main Screen form set up, with it's body consisting of one giant panel. I then have a series of User Controls which get erased or loaded into that Main Screen. What I don't know how to do is have the User Controls affect the Main Screen.

Code Example from a User Control:

PartsSystem parts = new PartsSystem(); //a user control
MainMenu.pnlMain.Controls.Clear();
MainMenu.pnlMain.Controls.Add(parts);

This code works when loading the main screen, but I can't seem to access pnlMain from my other user controls. Is it possible to fix this? I have a function set up to grab a user Control within the MainMenu form, but I can't seem to get it to be called from a user control. I'm trying to make it so that when they click a button, they go to the next screen (user control).

I am using C# on Windows 7 with Visual Studio 2010 and all the default settings.

Here is some additional code samples:

public partial class Main_Menu : Form
{
    public Main_Menu()
    {
        InitializeComponent();
        MainMenuPanel main = new MainMenuPanel();
        panelChange(main);
    }

    public void panelChange(UserControl newPanel)
    {
        pnlMain.Controls.Clear();
        pnlMain.Controls.Add(newPanel);
    }

}

That was Main_Menu. This code works on launch. Now, bringing up my PartsSystem is another issue.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Program_v1._0._0._0._4
{
public partial class MainMenuPanel : UserControl
{
    public MainMenuPanel()
    {
        InitializeComponent();
        Main_Menu parentForm = (this.Parent as Main_Menu);

    }

   public void btnPartsInventory_Click(object sender, EventArgs e)
    {
        Main_Menu myParent = (this.Parent as Main_Menu);
        PartsSystem parts = new PartsSystem();
        myParent.pnlMain.Controls.Clear(); //Object reference not set to an instance of an object.
 //This is the error I get at this point. It won't let me use the function.
        myParent.pnlMain.Controls.Add(parts); 

    }

}

}

Thank you for the help!

user3169698
  • 143
  • 5
  • 23

1 Answers1

0

Try using UserControl.Parent to get access the parent control.

access form from usercontrol

Get access to parent control from user control - C#

Community
  • 1
  • 1
Junaith
  • 3,298
  • 24
  • 34
  • UserControl.Parent doesn't seem to exist in my dropdown of choices. What am I missing in the UserControl to mark it as a child of my main form? – user3169698 Jan 13 '14 at 18:09
  • As given in the second link, you could have a property `Parent` in your usercontrol and set the Parent as MainForm. Use the Parent within the control to access the MainForm. – Junaith Jan 13 '14 at 18:15
  • Main_Menu parentForm = (this.Parent as Main_Menu); I added this in but it still won't let me access the pnlMain or the function I set up for it. – user3169698 Jan 13 '14 at 18:22
  • Use the type of pnlMain for casting instead of Main_Menu as you are adding the usercontrol to pnlMain – Junaith Jan 13 '14 at 18:34
  • Thank you for the help so far. I've updated my code. I get an error: "Object reference not set to an instance of an object." Whenever I try to use the myParent.panelChange(UserControl) function or the myParent.pnlMain.Controls.Clear(); – user3169698 Jan 13 '14 at 18:41
  • The typecast fails as you are using the wrong type(Main_Menu). Use the type of pnlMain to typecast then you can use `myParent.Controls.Clear()` instead of `myParent.pnlMain.Controls.Clear()` – Junaith Jan 13 '14 at 18:51
  • pnlMain is a Panel, if that's what you mean by the type. When changed to Panel myParent = (this.Parent as Panel); it gives me all kinds of errors. This is what I have now, but it stops at the if statement. Main_Menu myParent = (this.Parent as Main_Menu); PartsSystem parts = new PartsSystem(); if (this.Parent == null || this.Parent.GetType() != typeof(Main_Menu)) return; Panel myPanel = (this.Parent as Main_Menu).Controls["pnlMain"] as Panel; myPanel.Controls.Clear(); myPanel.Controls.Add(parts); – user3169698 Jan 13 '14 at 18:57