C# 隐藏和显示面板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13433812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:32:33  来源:igfitidea点击:

Hiding and showing panels

c#.netinstallerpanel

提问by Yuki Kutsuya

EDIT
I've found and posted the solution.

编辑
我找到并发布了解决方案。

I am trying to make an installer for my application and I am trying to do that with panels (I don't know if this is a good way of doing it, but this gives me more customization options instead of using the install shield program). What would be the best way to do this?
This is the code I have right know:

我正在尝试为我的应用程序制作一个安装程序,我正在尝试使用面板来做到这一点(我不知道这是否是一个好的方法,但这为我提供了更多的自定义选项,而不是使用安装屏蔽程序) . 什么是最好的方法来做到这一点?
这是我正确知道的代码:

C# Code

C# 代码

foreach (Control c in Controls)
        {
            if (c is Panel)
            {
                if (c.Name != "pnlBottom")
                {
                    if (c.Name.Contains(_currentPanel.ToString()))
                    {
                        c.Visible = true;
                        return;
                    }
                    else
                    {
                        c.Visible = false;
                    }
                }
            }
        }

采纳答案by CSharpie

Try this, it changes the Visibility of a single Panel:

试试这个,它会改变单个面板的可见性:

private void PanelVisible(string panelName, bool visible)
{
    var panel = this.Controls.OfType<Panel>().FirstOrDefault(p => p.Name == panelName);
    if (panel != default(Panel)) panel.Visible = visible;
}

If you want to make all Invisible, but one:

如果你想让所有的 Invisible,但一个:

private void PanelVisible(string panelName)
{
    foreach(var panel in this.Controls.OfType<Panel>().Where(p=>p.Name!="pnlBottom"))
    {
        panel.Visible = panel.Name == panelName;
    }
}

回答by Yuki Kutsuya

This is the code I use to show a panel:

这是我用来显示面板的代码:

private void ShowPanel(string panel)
    {
        foreach (Control c in Controls)
        {
            if (c is Panel)
            {
                if (c.Name != "pnlBottom")
                {
                    if (c.Name.Contains(panel))
                    {
                        c.Visible = true;
                        return;
                    }
                    else
                    {
                        c.Visible = false;
                    }
                }
            }
        }
    }

And this is the code I use to call it and browse through my panels:
Next button

这是我用来调用它并浏览我的面板的代码:
下一步按钮

private void btnNext_Click(object sender, EventArgs e)
    {
        if (pnlContent1.Visible) { ShowPanel("2"); return; }
        if (pnlContent2.Visible) { ShowPanel("3"); return; }
        if (pnlContent3.Visible) { ShowPanel("4"); return; }
        if (pnlContent4.Visible) { ShowPanel("5"); return; }
    }

Back button

返回键

private void btnBack_Click(object sender, EventArgs e)
    {
        if (pnlContent2.Visible) { ShowPanel("1"); return; }
        if (pnlContent3.Visible) { ShowPanel("2"); return; }
        if (pnlContent4.Visible) { ShowPanel("3"); return; }
        if (pnlContent5.Visible) { ShowPanel("4"); return; }
    }

I hope this will be use to someone else as well :D!

我希望这也能用于其他人:D!