繁体   English   中英

C#多种形式和面板

[英]C# Multiple Forms and Panels

我设置了一个主屏幕窗体,它的主体由一个巨大的面板组成。 然后,我有一系列用户控件,这些用户控件被擦除或加载到该主屏幕中。 我不知道该怎么做是让用户控件影响主屏幕。

用户控件中的代码示例:

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

加载主屏幕时,此代码有效,但是我似乎无法从其他用户控件访问pnlMain。 有可能解决这个问题吗? 我已经设置了一个函数,可以在MainMenu表单中获取用户控件,但是似乎无法从用户控件中调用它。 我试图做到这一点,以便当他们单击按钮时,他们进入下一个屏幕(用户控件)。

我在Windows 7和Visual Studio 2010以及所有默认设置上使用C#。

以下是一些其他代码示例:

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

}

那是Main_Menu。 此代码在启动时有效。 现在,启动我的PartsSystem是另一个问题。

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

    }

}

}

感谢您的帮助!

尝试使用UserControl.Parent获取对父控件的访问。

从用户控件访问表单

从用户控件获取对父控件的访问-C#

暂无
暂无

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

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