简体   繁体   English

应用程序Windows窗体C#使用菜单(用户控件)

[英]Application Windows Form C# use menu (User control)

I have an application with 2 Forms, for those forms I have create a Menu which I depose on the two forms. 我有一个带有2种表单的应用程序,对于这些表单,我创建了一个菜单,并将其放置在这两种表单上。

There is only a menuStrip item on the menu, I just want when I click on "test1" to redirect to Form1 and when I click to "test2" I want to redirect to Form2. 菜单上只有一个menuStrip项,我只想单击“ test1”以重定向到Form1,单击“ test2”时要重定向到Form2。

But if test1 is already open/display I don't want to show him again and the same for test2. 但是如果test1已经打开/显示,我不想再显示给他,并且与test2相同。

我的菜单

My code in my Menu : 我的菜单中的代码:

 public partial class Menu : UserControl
{
    public Menu()
    {
        InitializeComponent();
    }

    private void test1ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        Form2 f2 = new Form2();
        f2.Hide();
        f1.Hide();
        f1.ShowDialog();
    }

    private void test2ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        Form2 f2 = new Form2();
        f1.Hide();
        f2.Hide();
        f2.ShowDialog();
    }
}

My Form1 : 我的表格1: 表格1

My Form2 : 我的Form2: 表格2

I just want the same result like my Buttons in Form1 and Form2 : 我只想要像Form1和Form2中的Button一样的结果:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form1 f1 = new Form1();

        f1.ShowDialog();
    }

}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form2 f2 = new Form2();

        f2.ShowDialog();
    }
}

I thought that the property Visible for forms could help me but not... 我以为表单可见属性可以帮助我,但不能帮助我...

The problem is when I click on my buttons it's open a new window but when my form is already open I don't want to open it again. 问题是当我单击按钮时,它会打开一个新窗口,但是当我的表单已经打开时,我不想再次打开它。

Thanks for your reply, I hope that I am clear sorry for my english in advance. 感谢您的答复,我希望我先对我的英语表示抱歉。

You are currently creating a new form each time the click handler code is executed. 当前,每次执行单击处理程序代码时,您都在创建一个新表单。

Here is one way, but its nasty and I wouldn't really recommend it. 这是一种方法,但是它很讨厌,我不会真的推荐它。 I've assumed that form1 is the entry to your application and that its also the exit of the application. 我假设form1是应用程序的入口,并且它也是应用程序的出口。 This solution uses a singleton to hold the f1/f2 instances. 此解决方案使用单例来保存f1 / f2实例。

public static class Global
{
    static Global()
    {
        f2 = new Form2();
    }
    public static Form f1;
    public static Form f2;
}

Your menu altered: 您的菜单已更改:

public partial class Menu : UserControl
{
    public Menu()
    {
        InitializeComponent();
    }

    private void toolStripButton1_Click(object sender, EventArgs e)
    {
        Global.f2.Hide();
        Global.f1.Hide();
        Global.f1.Show();
    }

    private void toolStripButton2_Click(object sender, EventArgs e)
    {
        Global.f1.Hide();
        Global.f2.Hide();
        Global.f2.Show();
    }

    public void SetForm1(Form form)
    {
        Global.f1 = form;
    }

    public void SetForm2(Form form)
    {
        Global.f2 = form;
    }
}

And the forms: 以及形式:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Global.f1 = this;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Global.f2.ShowDialog();
    }
}
public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
        Global.f1.Show();
    }
}

Hope this helps. 希望这可以帮助。

Your logic is wrong. 你的逻辑是错误的。

It seems to me that you want to display different content depending on the user selection on that 'menuStrip'. 在我看来,您希望根据“ menuStrip”上的用户选择显示不同的内容。 You need to look at dynamic controls loading not different forms loading. 您需要查看动态控件加载,而不是不同的表单加载。

You can have just a MainForm with that 'menuStrip' and a Panel. 您只能拥有带有该“ menuStrip”和面板的MainForm。 Define some User Controls and dynamically add them to that panel based on the user selection. 定义一些用户控件,并根据用户选择将其动态添加到该面板中。

Snippet 片段

public MainForm : Form
{
    public MainForm()
    {
         // code
    }

    public void MenuStrip_OptionSelected(object sender, EventArgs e)
    {
        Panel1.Controls.Clear();
        switch(MenuStrip.SelectedValue)
        {
            case "UserControl1" : Panel1.Controls.Add(new UserControl1()); break;
            ...
        }
    }
}


public UserControl1 : UserControl
{
    // code
}

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

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