Tarek-Dev
Tarek-Dev

Reputation: 170

Showing and hiding panels on button click c#

I have 4 panels (on top of each others). i want to hide all panels and show one of them depending on the clicked button. when i launch the application and click on the button, it successfully hides all panels but it doesn't show the panel that i want. what am i doing wrong?

Here's my code:

namespace Detailing
{
public partial class MainForm : Form
{
    public void hidePanels()
    {
        welcomePanel.Width = 0;
        homePanel.Width = 0;
        historyPanel.Width = 0;
        savePanel.Width = 0;
    }
    public MainForm()
    {
        InitializeComponent();
        Load += new EventHandler(MainForm_Load);
    }
    private void MainForm_Load(object sender, EventArgs e)
    {
        hidePanels();
        welcomePanel.Width = 1306;
    }

    private void homeButton_Click(object sender, EventArgs e)
    {
        hidePanels();
        homePanel.Width = 1306;
    }
}
}

P.S. i tried to use welcomePanel.Hide(); and homePanel.Show(); and it didn't work. Also i tried to use welcomePanel.Visible = false; and homePanel.Visible = true; but sadly it didn't work as well.

Upvotes: 1

Views: 19637

Answers (2)

To make your code short just directly set your panel to false on their properties so and set to true only the welcomePanel. this the property of the panel just find the Visible then set it to false or true

namespace Detailing { public partial class MainForm : Form {

public MainForm()
{
    InitializeComponent();
    Load += new EventHandler(MainForm_Load);
}
private void homeButton_Click(object sender, EventArgs e)
{
    homePanel.Visible = true;
}

//and so on......

} }

Upvotes: 0

Steve
Steve

Reputation: 216293

A Panel is a control container. This means that, if (using the designer) you have dragged a panel over the surface of another panel, it becomes a child of the underlying panel. You can see easily this fact when you try to move the underlying panel. All the childs move together.

You can draw your panel in different position of the form and keep just one as a placeholder for all the other panels. When you load the form or in the form constructor you can move by code the other panels in the same Location of the reference panel.

So,assuming welcomePanel is the reference panel you can write:

public partial class MainForm : Form
{
    public void hidePanels()
    {
        welcomePanel.Visible = false;
        homePanel.Visible = false;
        historyPanel.Visible = false;
        savePanel.Visible = false;
    }
    public MainForm()
    {
        InitializeComponent();
        Load += new EventHandler(MainForm_Load);
        homePanel.Location = welcomePanel.Location;
        historyPanel.Location = welcomePanel.Location;
        savePanel.Location = welcomePanel.Location;        
    }
    private void MainForm_Load(object sender, EventArgs e)
    {
        hidePanels();
        welcomePanel.Visible = true;
    }

    private void homeButton_Click(object sender, EventArgs e)
    {
        hidePanels();
        homePanel.Visible = true;
    }
    ..... and so on ...

}

Another approach is to use a TabControl and show/hides the TabPage as you require

Upvotes: 2

Related Questions