带有几个复选框和面板的页面 [英] Page with several check boxes and panels

查看:93
本文介绍了带有几个复选框和面板的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有几个复选框的页面。

当选中一个选项时,面板会打开,但面板是不可见的。目前我正在使用几个_SelectedIndexChanged事件处理程序来显示面板何时可见。



这是其中之一

I have a page with several check boxes.
When a option is checked a panel opens othere wise the panel is invisible. Currently I am using several _SelectedIndexChanged event handlers to show when the panels are to be visible.

Here is one of them

protected void rblChanged_SelectedIndexChanged(object sender, EventArgs e)
    {
        string sSelect = rblChanged.SelectedItem.Text;
        switch (sSelect)
        {
            case "Yes":
                lblEditRow2.Visible = false;
                txtEditOLDASAF_Number.Visible = false;
                lblEditRow3.Visible = false;
                rblChanged.Visible = true;
                lblEditRow3.Visible = true;
                rblChanged.Visible = true;
                lblEditRow3a.Visible = true;
                lblEditRow4.Visible = false;
                radEditRow4a.Visible = false;
                txtEditSectionsAmended.Visible = false;
                sopPanel.Visible = false;
                lblEditRow6.Visible = false;
                rblAnimalManipulation.Visible = false; 
                manPanel.Visible = false;
                lblEditRow6a.Visible = false;
                rblEuthanized.Visible = false ;
                euthPanel.Visible = false;
                speciesPanel.Visible = false;
                COICasePanel.Visible = false;
                assurancePanel.Visible = false;
                fundingPanel.Visible = false;
                justificationPanel.Visible = false;
                housingPanel.Visible = false;
                environmentPanel.Visible = false;
                break;
            case "No":
                lblEditRow2.Visible = false;
                txtEditOLDASAF_Number.Visible = false;
                lblEditRow3.Visible = false;
                rblChanged.Visible = true;
                lblEditRow3.Visible = true;
                lblEditRow3a.Visible = false;
                lblEditRow4.Visible = false;
                radEditRow4a.Visible = false;
                txtEditSectionsAmended.Visible = false;
                sopPanel.Visible = false;
                lblEditRow6.Visible = false;
                rblAnimalManipulation.Visible = false; 
                manPanel.Visible = false;
                lblEditRow6a.Visible = false;
                rblEuthanized.Visible = false;
                euthPanel.Visible =false;
                speciesPanel.Visible = false;
                COICasePanel.Visible = false;
                assurancePanel.Visible = false;
                fundingPanel.Visible = false;
                justificationPanel.Visible = false;
                housingPanel.Visible = false;
                environmentPanel.Visible = false;
                break;
        }
    }



如果表单上有大量的复选框和单选按钮,这可能效率很低。

数据库中是否有一种方法可以确定何时打开或关闭面板


This can be very inefficient with the large amount of checkboxes and radio buttons on the form.
Is there a way in a database to determine when a panel is to be opened or closed

推荐答案

重构中明显的轻松获胜将是添加控件其可见性将设置为与Panel的ControlCollection相同的值,然后,只需使包含它们的Panel可见或隐藏。您可以使这些面板没有边框,并且具有与on显示的相同的背景颜色,如果这是一个问题。



进一步实现重构确实需要了解应用程序使用的不同UI状态(模式,如果你愿意)。



有许多数据结构可用于处理每次UI从一种模式更改为另一种模式时控件可见的自动化。我可能会使用像Dictionary< Enum,List< Control>>这样的东西进行原型设计。将UI状态映射到每个州应该可见的控件列表:



//仅用于教育目的的粗略草图:将按原样编译,并生成预期输出,但应被视为概念验证练习:
The obvious "easy win" in refactoring here would be to add Controls whose visibility is to be set to the same value to the ControlCollection of Panels, and to, then, simply make the Panel that contains them Visible or Hidden. You can make those Panels have no border, and have the same background color as whatever they are displayed "on," if that's a concern.

To get into more substantial refactoring really requires understanding the different UI states (modes, if you will) that your Application uses.

There are a number of data structures that could be used to handle automating what Controls are visible each time the UI changes from one mode to another. I might prototype using something like a Dictionary<Enum,List<Control>> to map UI states to the list of Controls which should be visible in each state:

// rough sketch for educational purposes only: will compile as is, and produce expected output, but should be regarded as only a proof-of-concept exercise:
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace StateChangeTest
{
    [Flags]
    public enum StateVisibility
    {
        State1 = 0x01,
        State2 = 0x02,
        State3 = 0x04,
        State4 = 0x08
    }

    public class StateMap
    {
        public Dictionary<StateVisibility,List<Control>> dctVisibleByState = new Dictionary<StateVisibility, List<Control>>();

        public StateMap()
        {
            dctVisibleByState.Add(StateVisibility.State1,
                new List<Control>
                {
                    new Button {Text = "button1"},
                    new RadioButton {Text = "radioButton1"}
                });

            dctVisibleByState.Add(StateVisibility.State3,
                new List<Control>
                {
                    new Button {Text = "button2"},
                    new RadioButton {Text = "radioButton2"}
                });


            dctVisibleByState.Add(StateVisibility.State4,
                new List<Control>
                {
                    new Button {Text = "button3"},
                    new RadioButton {Text = "radioButton2"}
                });
        }
    }
}

// sample test where you want the second, and third, set of Controls Visible, and the first set of Controls Hidden:

private void testStateMap()
{
    StateMap sMap = new StateMap();

    // define a StateVisibility value that is equivalent to #12
    StateVisibility sv12 = StateVisibility.State3 | StateVisibility.State4;

    foreach (StateVisibility sv in sMap.dctVisibleByState.Keys)
    {
        bool isVisibleInState = sv12.HasFlag(sv);    
        sMap.dctVisibleByState[sv].ForEach(cntrl => cntrl.Visible = isVisibleInState);
    }

    foreach (StateVisibility sv in sMap.dctVisibleByState.Keys)
    {
        Console.WriteLine(sv.ToString());

        foreach (Control cntrl in sMap.dctVisibleByState[sv])
        {
            Console.WriteLine("Control: {0} Visible: {1}", cntrl.Text, cntrl.Visible);
        }

        Console.WriteLine();
    }
}

// test output in Console:

State1
Control: button1 Visible: False
Control: radioButton1 Visible: False

State3
Control: button2 Visible: True
Control: radioButton2 Visible: True

State4
Control: button3 Visible: True
Control: radioButton2 Visible: True

注意a在所示的两种情况下,快速排序条件(在NotePad ++中)显示除了'lblEditRow3a之外,所有其他控件具有相同的赋值,并且有重复的赋值。两个案例陈述中的每个小组都设置为可见=假。所以你可以清理你的代码了很多:



COICasePanel.Visible = false;

COICasePanel.Visible = false;

assurancePanel.Visible = false;

assurancePanel.Visible = false;

environmentPanel.Visible = false;

environmentPanel.Visible = false;

euthPanel.Visible = false;

euthPanel.Visible = false;

fundingPanel.Visible = false;

fundingPanel.Visible = false;

housingPanel.Visible = false;

housingPanel.Visible = false;

justificationPanel。 Visible = false;

justificationPanel.Visible = false;

lblEditRow2.Visible = false;

lblEditRow2.Visible = false;

lblEditRow3.Visible = false;

lblEditRow3.Visible = false;

lblEditRow3.Visible = true;

lblEditRow3.Visible = true;

lblEditRow3a.Visible = false;

lblEditRow3a.Visible = true;

lblEditRow4.Visib le = false;

lblEditRow4.Visible = false;

lblEditRow6.Visible = false;

lblEditRow6.Visible = false;

lblEditRow6a.Visible = false;

lblEditRow6a.Visible = false;

manPanel.Visible = false;

manPanel.Visible = false;

radEditRow4a.Visible = false;

radEditRow4a.Visible = false;

rblAnimalManipulation.Visible = false;

rblAnimalManipulation.Visible = false;

rblChanged.Visible = true;

rblChanged.Visible = true;

rblChanged.Visible = true;

rblEuthanized.Visible = false;

rblEuthanized.Visible = false;

sopPanel.Visible = false;

sopPanel.Visible = false;

speciesPanel.Visible = false;

speciesPanel.Visible = false;

txtEditOLDASAF_Number.Visible = false;

txtEditOLDASAF_Number。 Visible = false;

txtEditSectionsAmended.Visible = false;

txtEditSectionsAmended.Visible = false;

Note that a quick sorting of your conditions (in NotePad++) in the two cases shown shows that with the exception of 'lblEditRow3a all other Controls have identical assignments, and there are repeated assignments. Every one of the Panels in the two case statements is set to 'Visible = false. So you can clean your code up quite a bit:

COICasePanel.Visible = false;
COICasePanel.Visible = false;
assurancePanel.Visible = false;
assurancePanel.Visible = false;
environmentPanel.Visible = false;
environmentPanel.Visible = false;
euthPanel.Visible = false;
euthPanel.Visible =false;
fundingPanel.Visible = false;
fundingPanel.Visible = false;
housingPanel.Visible = false;
housingPanel.Visible = false;
justificationPanel.Visible = false;
justificationPanel.Visible = false;
lblEditRow2.Visible = false;
lblEditRow2.Visible = false;
lblEditRow3.Visible = false;
lblEditRow3.Visible = false;
lblEditRow3.Visible = true;
lblEditRow3.Visible = true;
lblEditRow3a.Visible = false;
lblEditRow3a.Visible = true;
lblEditRow4.Visible = false;
lblEditRow4.Visible = false;
lblEditRow6.Visible = false;
lblEditRow6.Visible = false;
lblEditRow6a.Visible = false;
lblEditRow6a.Visible = false;
manPanel.Visible = false;
manPanel.Visible = false;
radEditRow4a.Visible = false;
radEditRow4a.Visible = false;
rblAnimalManipulation.Visible = false;
rblAnimalManipulation.Visible = false;
rblChanged.Visible = true;
rblChanged.Visible = true;
rblChanged.Visible = true;
rblEuthanized.Visible = false ;
rblEuthanized.Visible = false;
sopPanel.Visible = false;
sopPanel.Visible = false;
speciesPanel.Visible = false;
speciesPanel.Visible = false;
txtEditOLDASAF_Number.Visible = false;
txtEditOLDASAF_Number.Visible = false;
txtEditSectionsAmended.Visible = false;
txtEditSectionsAmended.Visible = false;


这篇关于带有几个复选框和面板的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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