2

我正在修补用户控件,我发现自己很难过。我的表单有 2 个用户控件,一个 TitleScreen 和一个 MainScreen。我需要在程序运行时让 TitleScreen 可见,然后在单击“新游戏”按钮时不可见,此时 MainScreen 需要可见。不幸的是,我无法弄清楚如何做到这一点。

FormMain.cs

    #region Property Region

    public UserControls.MainScreen ControlMainScreen
    {
        get { return controlMainScreen; }
    }

    #endregion

    #region Constructor Region

    public FormMain()
    {
        InitializeComponent();

        controlMainScreen.Visible = false;
    }

    #endregion 

TitleScreen.cs

    public void btnNewGame_Click(object sender, EventArgs e)
    {
        this.Visible = false;
        FormMain.ControlMainScreen.Visible = true;
    }

显然它不起作用,但我已经尝试了我知道该怎么做的一切。我也不知道如何使 controlMainScreen 成为静态,因为我实际上并没有做出声明(VS 做了)。任何帮助,将不胜感激。

4

2 回答 2

2

您可以使用ParentFormwhich is a property onUserControl来获取对父表单的引用。

((YourFormsExactType)(this.ParentForm)).ControlMainScreen.Visible = false;
于 2013-05-09T06:03:00.337 回答
1

在 Win 项目上添加用户控件,然后动态添加到 Form。

这是代码:

public Show_hideControl()
{
    InitializeComponent();

    //Adding First User control
    Main _main = new Main();        

    panel1.Controls.Add(_main);
}

private void button1_Click(object sender, EventArgs e)
{
   //removing already added user control
   panel1.Controls.Clear();            
   //Adding second user control
   Alternative _alter = new Alternative();
   panel1.Controls.Add(_alter);
}
于 2013-05-09T06:51:32.067 回答