面板不会显示何时跳过按钮。 [英] Panels wont show when button is skipped.

查看:133
本文介绍了面板不会显示何时跳过按钮。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拆分容器面板(面板1包含7个按钮,面板2来自拆分容器,包含面板3-9)



当点击按钮1时,它将显示面板3,单击按钮2将显示面板4 ....但每次跳过按钮时,它都不会显示单击按钮的面板。恩。我点击按钮1它将显示面板3,但当我跳过按钮2并单击按钮3它3它不会显示面板5 ...每次我跳过按钮它不会显示指定的面板... #WindowsAppForm C#



我尝试过:



I got a splitcontainer panel(panel 1 contains 7 buttons, panel2 from split container contains panels 3-9)

When button 1 is clicked it will show panel 3, button 2 when clicked will show panel 4.... but every time I skip a button it will not show the panel of the clicked button. ex. I clicked button 1 it'll show panel 3 but when I skip button 2 and click button 3 it 3 it wont display panel 5... every time I skip buttons it wont show the designated panels... #WindowsAppForm C#

What I have tried:

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

namespace kennyrogers
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            splitContainer1.Panel2.Visible = true;
            panel3.Visible = true;
            panel4.Visible = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            panel4.Visible = true;
            panel5.Visible = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            panel5.Visible = true;
            panel6.Visible = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            panel6.Visible = true;
            panel7.Visible = false;

        }

        private void button5_Click(object sender, EventArgs e)
        {
            panel7.Visible = true;
            panel8.Visible = false;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            panel8.Visible = true;
            panel9.Visible = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            panel9.Visible = true;
            panel6.Visible = false;
        }
    }
}

推荐答案

您已回答了自己的问题。如果查看代码,则不会为每个按钮管理所有面板可见性状态。例如:button6代码不会改变panel1的可见性状态,等等...



您可以使用国家设计模式 [ ^ ]或共享方法和switch语句。但对于像上面这样简单的事情,您可以执行以下操作:



You have answered your own question. If you look at the code, not all panel visibility states are managed for each button. eg: button6 code does not change the panel1 visibility state, and so on...

You can either use the State Design Pattern[^] or a shared method and switch statement. But for something simple like above, you could do the following:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        SetState(States.first);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SetState(States.first);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        SetState(States.second);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        SetState(States.third);
    }

    private void button4_Click(object sender, EventArgs e)
    {
        SetState(States.forth);
    }

    private void button5_Click(object sender, EventArgs e)
    {
        SetState(States.fifth);
    }

    private void button6_Click(object sender, EventArgs e)
    {
        SetState(States.sixth);
    }

    private void button7_Click(object sender, EventArgs e)
    {
        SetState(States.seventh);
    }

    private void button8_Click(object sender, EventArgs e)
    {
        SetState(States.eighth);
    }

    private void button9_Click(object sender, EventArgs e)
    {
        SetState(States.nineth);
    }

    private void SetState(States state)
    {
        panel1.Visible = state == States.first;
        panel2.Visible = state == States.second;
        panel3.Visible = state == States.third;
        panel4.Visible = state == States.forth;
        panel5.Visible = state == States.fifth;
        panel6.Visible = state == States.sixth;
        panel7.Visible = state == States.seventh;
        panel8.Visible = state == States.eighth;
        panel9.Visible = state == States.nineth;
    }
}

enum States
{
    first,
    second,
    third,
    forth,
    fifth,
    sixth,
    seventh,
    eighth,
    nineth
}



**更新:如果您需要在按钮上单击显示多个面板,则可以使用<$ c $的枚举按位操作c>标志属性。例如:


** UPDATE: If you need to have more than one panel shown on a button click, you could use enum bitwise operation with the Flags attribute. Eg:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        SetState(States.None);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SetState(States.first | States.third);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        SetState(States.second);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        SetState(States.third | States.second);
    }

    private void SetState(States state)
    {
        panel1.Visible = (state & States.first) != 0;
        panel2.Visible = (state & States.second) != 0;
        panel3.Visible = (state & States.third) != 0;
        // and so on...
    }
}

[Flags]
internal enum States
{
    None = 0,
    first = 1,
    second = 2,
    third = 4,
    forth = 8,
    fifth = 16,
    sixth = 32,
    seventh = 64,
    eighth = 128,
    nineth = 256
}


Note 0. Note that the Panel Control in WinForms is not a full-featured Window with a handle: it does not receive Key events, it will not become Focused as other Controls will. The code in the second example shows one way to get around this by setting Focus to some other Control inside the Panel which is a "full-windowed Control."

最简单的方法是,在设计时,选择将对Panel执行操作的所有按钮,打开Properties Explorer,选择Events,然后双击'Click事件:现在VS为您编写Click EventHandler,您所要做的就是写一个Switch语句:

The simplest way to do this is to, at design-time, select all your Buttons that will do something to the Panels, open the Properties explorer, select Events, and double-click on the 'Click event: now VS writes the Click EventHandler for you, and all you have to do is write a Switch statement:

private void button1_Click_1(object sender, EventArgs e)
{
    switch ((sender as Button).Name)
    {
        case "buttonl":
            splitContainer1.Panel2.Visible = true;
            panel3.Visible = true;
            panel4.Visible = false;
            break;
        case "button2":
            // do whatecver
            break;
        
        // and so on ...

        default:
            break;
    }
}

Note: 1. A slightly more complex technique: map the Buttons in the left SplitterPanel to the Panels in the right SplitterPanel using a Dictionary where the Key is a Button, and the Value is an executable function that takes a Panel as a parameter: Dictionary<Button, Action>

public Dictionary<Button, Action> ButtonToAction;

private void Form1_Load(object sender, EventArgs e)
{ 
    ButtonToAction = new Dictionary<Button, Action>
    {
        { button1, () => PanelSelect(panel3)},
        { button2, () => PanelSelect(panel4)},
        { button3, () => PanelSelect(panel5)},
        { button4, () => PanelSelect(panel6)}
    };
}

private void PanelSelect(Panel panel)
{
    // see Note #0 above
    ActiveControl = panel.Controls[0];
    panel.Controls[0].Focus();

    switch (panel.Name)
    {
        case "panel3":
            splitContainer1.Panel2.Visible = true;
            panel3.Visible = true;
            panel4.Visible = false;
            break;
        case "panel4":
            break;
        case "panel5":
            break;
        case "panel6":
            break; 
        default:
            break;         
    }
}


这篇关于面板不会显示何时跳过按钮。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆