Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
2.71/5 (4 votes)
See more:
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
C#
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
Posted
Updated 30-May-14 5:47am
v2
Comments
gggustafson 30-May-14 13:10pm    
What is "rblChanged"? A RadioButton? A CheckBox? A ComboBox? I assume a ComboBox because RadioButtons and CheckBoxes do not have SelectedIndexChanged event.

You should be using a CheckBox here and handle the CheckBox.CheckedChanged event. Also your code contains duplicate code. It might be better if, on entry into the event handler, you set all of the form's control properties to say false. Then in each of the two cases, set only those properties to true that are appropriate.

In so far as your question regarding use of a database vs use of form controls, the IO required is multiple hundreds of times more expensive in IO than in memory. Your concern that elliciency is affected is premature. Until you can show inefficiency by profiling your application, such concerns are better not considered.
Why inefficient?
Member 10854933 30-May-14 13:14pm    
rblChanged is a RadioBoxList

There are a few CheckBoxList objects as well
Member 10854933 30-May-14 15:45pm    
I am hoping to find a better way to do this because I am developing this web form from a PDF file. When completed the PDF file is about 21 pages long, and has numerous (close to 100) check boxes and radio buttons, most of which open another section when clicked.

So far I am on about page 7 of the PDF file, and was wondering if someone has a better way than a SelectEventChanged event for each one.

1 solution

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:
C#
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
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;
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900