简体   繁体   English

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

[英]How to open usercontrol in a click of a button C#

Firstly I had been searching about my problem and can't find \\help.首先,我一直在寻找我的问题,但找不到 \\help。 So my question is I've got 3 buttons and three userControl and 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.所以我的问题是我有 3 个按钮和三个userControl ,当我单击一个按钮时,它显示 usercontrol 1,但在我单击按钮 2 后。我无法回到 usercontrol 1 我卡在 usercontrol2 和按钮 1 不做任何事情了。 Here's my code:这是我的代码:

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

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.在构造函数public Form2()而不是在事件处理程序中。 This will allow you to add这将允许您添加

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

in your button1_Click handler.在您的button1_Click处理程序中。

You should probably also add u3.Hide() to button2_Click .您可能还应该将u3.Hide()添加到button2_Click

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.请注意,我创建了一个面板来存储用户控件,但我认为如果直接在 Windows 窗体上显示用户控件也是一样的。 you only need to hide your controls.你只需要隐藏你的控件。

If you use BringToFront() property when clicking the button, the functionality would happen.如果您在单击按钮时使用了BringToFront() 属性,则会发生该功能。 This method will be effective for response button clicks other than Hide() property.此方法对 Hide() 属性以外的响应按钮点击有效。

private void buttonProdutsList_Click(object sender, EventArgs e)
    {
        productsListView.BringToFront();
    }

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

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