Switch between panels

I have 3 panels in 1 form to go through a process to enter certain data. When the next button on a panel is clicked, the next panel should be displayed. Initially I turned on the visibility of the first panel and turned off the visibility of the other panels.
When the next button is clicked, the following code will be executed.

      panel1.Visible = false;
      panel2.Visible = true;

      

For development purposes, I put them side by side (not one on top of the other) and he achieved perfection.
But when I put them one on top of the other, this code didn't seem to be a wok, which means that when the next button is pressed, it just shows an empty form.
Then I added the code below.

    panel1.SendToBack();
    panel2.BringToFront();

      

But it didn't work. Can anyone help me with this. Thank.

+3


source to share


3 answers


This always goes wrong in the designer, the bottom panel will become the parent of the top one. Therefore, if you hide the bottom one, you will never see the top view.

This can be worked around with View> (Other Windows)> Document Outline, drag the top pane back into the form. Still quite painful, you usually have to edit the location manually, and making any changes to the form in the designer later causes the panel to roll back.



There are better ways to do this. It is highly recommended to create UserControls, they have their own design surface. Or use the RAD way and do it with TabControl. All you need to do is to hide the tab at run time, the theme of A + the Q .

+5


source


You have to be careful when stacking container controls like Panel

'one on top of the other.

In the designer, you can do this, but only by gently moving the panel with the keyboard . Using the mouse will always move one in not on the other hand, when the upper-left corner is inside the other.



Alternatively, you can navigate through the code.

Running this code has the advantage that it can still work with the bottom panels and its contents. Sometimes I put them in tabs (at runtime invisible) dummy tab and move it in or out of pages to hide and show them.

+4


source


Here is some code you can use to have multiple panels at the same time and switch between them with the next added form to the form.

public Form1()
    {
        InitializeComponent();
        panel1.Visible = true;
        panel3.Visible = false;
        panel2.Visible = false;
    }

    private void btnNext_Click(object sender, EventArgs e)
    {
        if (panel1.Visible)
        {
            panel1.Visible = false;
            panel2.Visible = true;
            panel3.Visible = false;
        }
        else if (panel2.Visible)
        {
            panel1.Visible = false;
            panel2.Visible = false;
            panel3.Visible = true;
        }
        else if (panel3.Visible)
        {
            panel1.Visible = true;
            panel2.Visible = false;
            panel3.Visible = false;
        }
    }

      

Of course if you tagged your panels in ascending / descending format there is no break between the two tags like 1,2,3,4 or 5,4,3,2 and not 1,2,4 you can use this code

public Form1()
    {
        InitializeComponent();
        panel1.Visible = true;
        panel2.Visible = false;
        panel3.Visible = false;
    }

    private void btnNext_Click(object sender, EventArgs e)
    {
        TogglePanels();
    }
    public void TogglePanels()
    {
        List<Panel> allPanelsInForm = new List<Panel>();
        foreach (var control in Controls)
        {
            if (control is Panel)
                allPanelsInForm.Add(control as Panel);
        }
        Panel visiblePanel = allPanelsInForm.Where(o => o.Visible).FirstOrDefault();
        int nextPanelId = Convert.ToInt32(visiblePanel.Tag) + 1;
        bool nextPanelExists = allPanelsInForm.Exists(o => Convert.ToInt32(o.Tag) == nextPanelId);
        nextPanelId = nextPanelExists ? nextPanelId : 1;
        foreach (Panel panel in allPanelsInForm)
        {
            panel.Visible = Convert.ToInt32(panel.Tag) == nextPanelId ? true : false;
        }
    }

      

I would like this to help you.

+1


source







All Articles