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!