winforms 在C#中导航不同的窗体

bxgwgixi  于 2023-03-13  发布在  C#
关注(0)|答案(1)|浏览(171)

我最近开始使用C#开发一个Windows窗体的应用程序。要浏览我正在使用的不同窗体:

Form Form_name = new Form ();
        Form_name .TopLevel = false;
        panel1.Controls.Clear();
        panel1.Controls.Add(Form_name);
        Form_name.Show();

当前的设置是,我有主菜单表单,您可以在其中导航到上传照片表单。但在上传照片中上传图像后,我想显示下一个表单。但由于我们现在正在另一个表单中工作,因此无法用途:

panel1.Controls.Clear();
panel1.Controls.Add(Form_name);

因为panel 1在第一个表单中被引用了,所以我尝试创建一个公共类来清除它,但是没有成功。
我的问题归结为:

  • 是否有更好的方法在不同的表单/窗口中导航?
  • 我们可以创建一个公共函数来清除第一个窗体中的面板吗?

MainMenu.cs

private void mainMenu1_Click(object sender, EventArgs e)
{
    ME_Upload ME_UploadConstruct = new ME_Upload();
    ME_UploadConstruct.TopLevel = false;
    panel1.Controls.Clear();
    panel1.Controls.Add(ME_UploadConstruct);
    ME_UploadConstruct.Show();
}

private void mainMenu2_Click(object sender, EventArgs e)
{
    IndividualEditor individualEditor = new IndividualEditor();
    individualEditor.TopLevel = false;
    panel1.Controls.Clear();
    panel1.Controls.Add(individualEditor);
    individualEditor.Show();
}

}
ME_Upload.cs

private void ME_UploadButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
            if (opf.ShowDialog() == DialogResult.OK)
            {
                ME_ImageEditor ME_ImageEditorConstruct = new ME_ImageEditor();
                ME_ImageEditorConstruct.TopLevel = false;
                panel1.Controls.Clear(); ///Not Working
                panel1.Controls.Add(ME_ImageEditorConstruct);  ///Not Working
                ME_ImageEditorConstruct.Show();
            }
        }
ax6ht2ek

ax6ht2ek1#

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        menu1.TopLevel = False
        Panel1.Controls.Clear()
        Panel1.Controls.Add(menu1)
        menu1.Show()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        menu3.TopLevel = False
        Panel1.Controls.Clear()
        Panel1.Controls.Add(menu3)
        menu3.Show()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        menu2.TopLevel = False
        Panel1.Controls.Clear()
        Panel1.Controls.Add(menu2)
        menu2.Show()
    End Sub
End Class

相关问题