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

问题描述

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

当选中一个选项时,面板会打开,但面板是不可见的。目前我正在使用几个_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

推荐答案

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;


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

08-15 03:58