Switch between panels

橙三吉。 提交于 2019-12-01 06:27:50

This always goes wrong in the designer, the bottom panel will become the Parent of the top one. So if you hide the bottom one you can never see see the top one.

This can be worked around with View > (Other Windows) > Document Outline, drag the top panel back to the form. Still pretty painful, you typically have to edit the Location by hand and making any changes to the form in the designer later tends to slurp the panel back.

There are better ways to do this. Creating UserControls instead is highly recommended, they have their own design surface. Or use the RAD way and do this with a TabControl instead. All you have to do is hide the tabs at runtime, the subject of this Q+A.

You have to be careful when putting container controls like a Panel 'one upon another '.

In the designer you can do it, but only by carfully moving the panel with the keyboard. Using the mouse will always put the moved one into not upon the other one whenever its top left corner gets inside the other one.

As an alternative you can do the moving in code.

Doing it in code has the advantage of still being able to work with the lower panels and its content. Sometimes I stuff them into the tabpages of an (at runtime invisible) dummy tab and move it in or out of the pages to hide and show them..

Here is the code you can use to have multiple panels at the same time and switch between them with the next buttom added 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 no gap between the tags for example 1,2,3,4 or 5,4,3,2 not 1,2,4 you could 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 wish it could help you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!