在窗体中切换视图-使用面板c#

在窗体中切换视图-使用面板c#,c#,C#,使用按钮更改表单项目的视图。 如您所见,每个按钮中都会有大量文本 private void start_Click(object sender, EventArgs e) { panel1.Visible = true; panel2.Visible = false; panel3.Visible = false; panel4.Visible = false; } 因此,我们制定了一个方法。还是觉得很笨拙。。是否有更好的方法切

使用按钮更改表单项目的视图。 如您所见,每个按钮中都会有大量文本

private void start_Click(object sender, EventArgs e)

    {
      panel1.Visible = true;
      panel2.Visible = false;
      panel3.Visible = false;
      panel4.Visible = false;
    }
因此,我们制定了一个方法。还是觉得很笨拙。。是否有更好的方法切换每个面板的视图

    private void start_Click(object sender, EventArgs e)
    {
      panel = 1;
      PanelW(1);
    }

    public void PanelW(int panel)
    {
        if (panel == 1)
        {
            panel1.Visible = true;
            panel2.Visible = false;
            panel3.Visible = false;
            panel4.Visible = false;
        }
        else if (panel == 2)
        {
            panel2.Visible = true;
            panel1.Visible = false;
            panel3.Visible = false;
            panel4.Visible = false;
        }
        else if (panel == 3)
        {
            panel3.Visible = true;
            panel1.Visible = false;
            panel2.Visible = false;
            panel4.Visible = false;
        }
        else if (panel == 4)
        {
            panel4.Visible = true;
            panel1.Visible = false;
            panel2.Visible = false;
            panel3.Visible = false;
        }

我不想使用tabcontrol。不确定是否最好也使用返回值按钮,而不是void。

有很多方法可以做到这一点。在我看来,您可以将子类面板添加到其中,然后使用for循环根据该属性设置可见性。次级面板:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public enum PanelType
    {
        HomeScreen, Settings
    }
    public partial class CustomPanel : Panel
    {

        public PanelType PanelType { get; set; }

    }
}
然后是切换面板的方法:

        private void button1_Click(object sender, EventArgs e)
        {
            SwitchPanel(PanelType.HomeScreen);
        }

        private void SwitchPanel(PanelType displayType)
        {
            foreach (var ctl in this.Controls)
            {
                if (ctl.GetType() == typeof(CustomPanel))
                    ((CustomPanel)ctl).Visible = ((CustomPanel)ctl).PanelType == displayType;
            }
        }

然后,您需要将现有面板替换为自定义面板(或您所称的任何面板),然后在设计器中,在每个面板集上替换其面板类型。

有很多方法可以做到这一点。在我看来,您可以将子类面板添加到其中,然后使用for循环根据该属性设置可见性。次级面板:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{

    public enum PanelType
    {
        HomeScreen, Settings
    }
    public partial class CustomPanel : Panel
    {

        public PanelType PanelType { get; set; }

    }
}
然后是切换面板的方法:

        private void button1_Click(object sender, EventArgs e)
        {
            SwitchPanel(PanelType.HomeScreen);
        }

        private void SwitchPanel(PanelType displayType)
        {
            foreach (var ctl in this.Controls)
            {
                if (ctl.GetType() == typeof(CustomPanel))
                    ((CustomPanel)ctl).Visible = ((CustomPanel)ctl).PanelType == displayType;
            }
        }

然后,您需要将现有面板替换为自定义面板(或任何您称之为面板的名称),然后在设计器中,在每个面板集上替换其面板类型。

您可以使用多种方法来完成此操作,例如:

public void PanelW(int panel)
{
    foreach (var pb in this.Controls.OfType<Panel>())
        pb.Visible = pb.Name == "panel" + panel;
}

您可以使用多种方法来执行此操作,例如:

public void PanelW(int panel)
{
    foreach (var pb in this.Controls.OfType<Panel>())
        pb.Visible = pb.Name == "panel" + panel;
}

很不错的!非常感谢,伙计。非常好!谢谢你,伙计。