简体   繁体   English

在表单中切换视图-使用面板C#

[英]Switch View in form - using panel c#

Using buttons to change the view of form Projects. 使用按钮更改表单项目的视图。 As you see, there will be a lot of text in each button. 如您所见,每个按钮中都会有很多文本。

private void start_Click(object sender, EventArgs e)

    {
      panel1.Visible = true;
      panel2.Visible = false;
      panel3.Visible = false;
      panel4.Visible = false;
    }

So made a method of it. 因此提出了一种方法。 Still feels very clumsy.. Is there a better way to switch the view of each panel? 仍然感觉很笨拙。是否有更好的方法来切换每个面板的视图?

    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;
        }

I dont want to use tabcontrol. 我不想使用tabcontrol。 Not sure if better using return value to button also, instead of void. 不知道是否最好也使用button的返回值,而不是void。

You can use a many way to do this for example : 您可以使用多种方式执行此操作,例如:

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

or use linq like as : 或者像这样使用linq:

public void PanelW(int panel)
{
    Controls.OfType<Panel>().Count(p => (p.Visible = p.Name == "panel" + panel));
}

Note : proposition 1 and 2 work if you named your Panels as the question (panel1, panel2, panel3, panel4 ..) 注意:如果您将面板命名为问题(面板1,面板2,面板3,面板4 ..),则命题1和命题2起作用。

or simply if you did not alot of Panels 或者只是如果您没有很多面板

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

There are many ways to do this. 有很多方法可以做到这一点。 Just off the top of my head, you could sub class Panel and add a property to it then use a for loop to set visibility based on that property. 就在我脑海中,您可以将Panel子类化,并为其添加一个属性,然后使用for循环基于该属性设置可见性。 Sub-classed panel: 子面板:

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; }

    }
}

And then the method to switch panels: 然后切换面板的方法:

        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;
            }
        }

You will then need to replace your existing panels with custom panels (or whatever you call them) and then in the designer, on each panel set it's panel type. 然后,您需要用自定义面板(或任何您称呼的面板)替换现有面板,然后在设计器中的每个面板上将其设置为面板类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM