如何通过单击按钮打开用户控件 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12122662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:09:14  来源:igfitidea点击:

How to open usercontrol in a click of a button C#

c#buttonuser-controls

提问by Kraze

Firstly I had been searching about my problem and can't find \help. So my question is I've got 3 buttons and three userControland when I click on one button it displays usercontrol 1 but after I click button 2. I cannot get back to usercontrol 1 im stuck in usercontrol2 and the button 1 does not do anything anymore. Here's my code:

首先,我一直在寻找我的问题,但找不到 \help。所以我的问题是我有 3 个按钮和三个按钮,userControl当我单击一个按钮时,它会显示 usercontrol 1,但是在我单击按钮 2 之后。我无法回到 usercontrol 1 中,我卡在 usercontrol2 中,而按钮 1 不再执行任何操作. 这是我的代码:

public partial class Form2 : Form
{
    UserControl1 u1;
    UserControl2 u2;
    UserControl3 u3;
    public Form2()
    {

        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        u1 = new UserControl1();
        u1.Dock = DockStyle.Fill;
        this.Controls.Add(u1);

    }

    private void button2_Click(object sender, EventArgs e)
    {
        u1.Hide();

        u2 = new UserControl2();
        u2.Dock = DockStyle.Fill;
        this.Controls.Add(u2);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        u1.Hide();
        u2.Hide();
        u3 = new UserControl3();
        u3.Dock = DockStyle.Fill;
        this.Controls.Add(u3);
    }

    private void button4_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

SOLVED CODE for other's who need :) --->

为其他需要的人解决代码:) --->

enter code here
public partial class Form2 : Form
{
    UserControl1 u1;
    UserControl2 u2;
    UserControl3 u3;
    public Form2()
    {
        u1 = new UserControl1();
        u2 = new UserControl2();
        u3 = new UserControl3();
                    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
            {
                u2.Hide();
                u3.Hide();
                u1.Show();
                u1.Dock = DockStyle.Fill;
              this.Controls.Add(u1);
          }
    private void button2_Click(object sender, EventArgs e)
    {
        u1.Hide();
        u3.Hide();
        u2.Show();
        u2.Dock = DockStyle.Fill;
        this.Controls.Add(u2);
    }
    private void button3_Click(object sender, EventArgs e)
    {
        u1.Hide();
        u2.Hide();
        u3.Show();
        u3.Dock = DockStyle.Fill;
        this.Controls.Add(u3);
    }

    private void button4_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

回答by Qi Chen

It seems that you should have:

看来你应该有:

u1=new UserControl1();
u2=new UserControl2();
u3=new UserControl3();

in the constructor public Form2()rather than in the event handlers. This will allow you to add

在构造函数中public Form2()而不是在事件处理程序中。这将允许您添加

u2.Hide();
u3.Hide();

in your button1_Clickhandler.

在你的button1_Click处理程序中。

You should probably also add u3.Hide()to button2_Click.

您可能还应该添加u3.Hide()button2_Click.

回答by varg

Take a look at this. I think it's absolutely clear and didn't need any further explanation.

看看这个。我认为这绝对清楚,不需要任何进一步的解释。

public partial class Form1 : Form
{
    private UserControl1 uc1 = new UserControl1();
    private UserControl2 uc2 = new UserControl2();
    private UserControl3 uc3 = new UserControl3();

    public Form1()
    {
        InitializeComponent();
        AssignedButtonClickEvents();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    protected void ButtonClicked(object sender, EventArgs e)
    {
        Button button = sender as Button;
        panel1.Controls.Clear();

        if (button != null)
        {
            switch (button.Name)
            {
                case "button1":
                    uc1.Dock = DockStyle.Fill;
                    panel1.Controls.Add(uc1);
                    break;

                case "button2":
                    uc2.Dock = DockStyle.Fill;
                    panel1.Controls.Add(uc2);
                    break;

                case "button3":
                    uc3.Dock = DockStyle.Fill;
                    panel1.Controls.Add(uc3);
                    break;

                default:
                    panel1.Controls.Clear();
                    break;
            }
        }

    }

    public void AssignedButtonClickEvents()
    {
        foreach (Control ctl in this.Controls)
        {
            if (ctl is Button)
            {
                Button button = (Button)ctl;
                button.Click += new EventHandler(ButtonClicked);
            }
        }
    }

edit
note that i create a panel to store the usercontrols, but i think it's the same if you show your user controls directly on the windows forms. you only need to hide your controls.

编辑
请注意,我创建了一个面板来存储用户控件,但我认为如果您直接在 Windows 窗体上显示用户控件也是一样的。你只需要隐藏你的控件。