Kraze
Kraze

Reputation: 11

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

Firstly I had been searching about my problem and can't find \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. 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();
    }
}

Upvotes: 1

Views: 31623

Answers (3)

Thusitha Wijerathne
Thusitha Wijerathne

Reputation: 19

If you use BringToFront() property when clicking the button, the functionality would happen. This method will be effective for response button clicks other than Hide() property.

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

Upvotes: 1

varg
varg

Reputation: 3636

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.

Upvotes: 2

Qi Chen
Qi Chen

Reputation: 1718

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

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

in your button1_Click handler.

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

Upvotes: 3

Related Questions