2
\$\begingroup\$

I have few controls on my page which I want to show/hide and/or enable/disable on some condition.

Images to help illustrate the requirements:

screenshot1 screenshot2 screenshot3

Following is the code to show/hide, enable/disable the controls. Please review it and tell me how I can extract common code.
Any other suggestion are also more than welcome.

// Change controls status (enable/disable, show/hide) on the basis of selected item
protected void ddlLocationType_SelectedIndexChanged(object sender, EventArgs e)
{
 int locationType = 0;
 int.TryParse(ddlLocationType.SelectedItem.Value, out locationType);
 EnableRelavantLocaitonControls(locationType);
}
private void EnableRelavantLocaitonControls(int locationType)
{
 switch (locationType)
 {
 case 0:
 DefaultControlsPosition();
 break;
 case 1:
 EnableControlsForProvince();
 break;
 case 2:
 EnableControlsForDistrict();
 break;
 case 3:
 EnableControlsForTehsil();
 break;
 case 4:
 EnableControlsForUC();
 break;
 case 6:
 EnableControlsForVillage();
 break;
 default:
 DefaultControlsPosition();
 break;
 }
}
// Show all drop downs.
// Hide all textboxes and disable all controls.
private void DefaultControlsPosition()
{
 ddlProvince.Enabled = false;
 ddlProvince.Visible = true;
 txtProvince.Enabled = false;
 txtProvince.Visible = false;
 ddlDistrict.Enabled = false;
 ddlDistrict.Visible = true;
 ddlDistrict.Enabled = false;
 txtDistrict.Visible = false;
 ddlTehsil.Enabled = false;
 ddlTehsil.Visible = true;
 txtTehsil.Enabled = false;
 txtTehsil.Visible = false;
 ddlUC.Enabled = false;
 ddlUC.Visible = true;
 txtUC.Enabled = false;
 txtUC.Visible = false;
 ddlVillage.Enabled = false;
 ddlVillage.Visible = true;
 txtVillage.Enabled = false;
 txtVillage.Visible = false;
}
// Show all drop downs except province drop down and disable all.
// Show and enable TextBox for province instead of drop down so user can 
// enter name of province.
private void EnableControlsForProvince()
{
 ddlProvince.Enabled = false;
 ddlProvince.Visible = false;
 txtProvince.Enabled = true;
 txtProvince.Visible = true;
 ddlDistrict.Enabled = false;
 ddlDistrict.Visible = true;
 ddlDistrict.Enabled = false;
 txtDistrict.Visible = false;
 ddlTehsil.Enabled = false;
 ddlTehsil.Visible = true;
 txtTehsil.Enabled = false;
 txtTehsil.Visible = false;
 ddlUC.Enabled = false;
 ddlUC.Visible = true;
 txtUC.Enabled = false;
 txtUC.Visible = false;
 ddlVillage.Enabled = false;
 ddlVillage.Visible = true;
 txtVillage.Enabled = false;
 txtVillage.Visible = false;
}
// Show and Enable 'Province' drop down.
// Hide and Disable 'District' drop down.
// Show and Enable 'District' text box.
// Show and Disable all other drop downs beneath District.
private void EnableControlsForDistrict()
{
 ddlProvince.Enabled = true;
 ddlProvince.Visible = true;
 txtProvince.Enabled = false;
 txtProvince.Visible = false;
 ddlDistrict.Enabled = false;
 ddlDistrict.Visible = false;
 txtDistrict.Enabled = true;
 txtDistrict.Visible = true;
 ddlTehsil.Enabled = false;
 ddlTehsil.Visible = true;
 txtTehsil.Enabled = false;
 txtTehsil.Visible = false;
 ddlUC.Enabled = false;
 ddlUC.Visible = true;
 txtUC.Enabled = false;
 txtUC.Visible = false;
 ddlVillage.Enabled = false;
 ddlVillage.Visible = true;
 txtVillage.Enabled = false;
 txtVillage.Visible = false;
}
private void EnableControlsForTehsil()
{
 ddlProvince.Enabled = true;
 ddlProvince.Visible = true;
 txtProvince.Enabled = false;
 txtProvince.Visible = false;
 ddlDistrict.Enabled = true;
 ddlDistrict.Visible = true;
 txtDistrict.Enabled = false;
 txtDistrict.Visible = false;
 ddlTehsil.Enabled = false;
 ddlTehsil.Visible = false;
 txtTehsil.Enabled = true;
 txtTehsil.Visible = true;
 ddlUC.Enabled = false;
 ddlUC.Visible = true;
 txtUC.Enabled = false;
 txtUC.Visible = false;
 ddlVillage.Enabled = false;
 ddlVillage.Visible = true;
 txtVillage.Enabled = false;
 txtVillage.Visible = false;
}
private void EnableControlsForUC()
{
 ddlProvince.Enabled = true;
 ddlProvince.Visible = true;
 txtProvince.Enabled = false;
 txtProvince.Visible = false;
 ddlDistrict.Enabled = true;
 ddlDistrict.Visible = true;
 txtDistrict.Enabled = false;
 txtDistrict.Visible = false;
 ddlTehsil.Enabled = true;
 ddlTehsil.Visible = true;
 txtTehsil.Enabled = false;
 txtTehsil.Visible = false;
 ddlUC.Enabled = false;
 ddlUC.Visible = false;
 txtUC.Enabled = true;
 txtUC.Visible = true;
 ddlVillage.Enabled = false;
 ddlVillage.Visible = true;
 txtVillage.Enabled = false;
 txtVillage.Visible = false;
}
private void EnableControlsForVillage()
{
 ddlProvince.Enabled = true;
 ddlProvince.Visible = true;
 txtProvince.Enabled = false;
 txtProvince.Visible = false;
 ddlDistrict.Enabled = true;
 ddlDistrict.Visible = true;
 txtDistrict.Enabled = false;
 txtDistrict.Visible = false;
 ddlTehsil.Enabled = true;
 ddlTehsil.Visible = true;
 txtTehsil.Enabled = false;
 txtTehsil.Visible = false;
 ddlUC.Enabled = true;
 ddlUC.Visible = true;
 txtUC.Enabled = false;
 txtUC.Visible = false;
 ddlVillage.Enabled = false;
 ddlVillage.Visible = false;
 txtVillage.Enabled = true;
 txtVillage.Visible = true;
}
Adam
5,2161 gold badge30 silver badges47 bronze badges
asked Jul 11, 2012 at 13:32
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You need to put your requirements in words here or a embedded picure... you can't expect us to click through your links just to figure out what you're doing... \$\endgroup\$ Commented Jul 11, 2012 at 14:02

2 Answers 2

4
\$\begingroup\$

First possible refactoring: define an enumeration for the three possible states

enum InputState{
 Hidden,
 Disabled,
 Enabled
}

and use it with an extension method (or by extending the class)

public static void SetVisibility(this Control ctrl, InputState state){
 switch(state){
 case InputState.Hidden:
 ctrl.Enabled = false;
 ctrl.Visible = false;
 break;
 case InputState.Disabled:
 ctrl.Enabled = false;
 ctrl.Visible = true;
 break;
 case InputState.Enabled:
 ctrl.Enabled = true;
 ctrl.Visible = true;
 break;
 }
}
answered Jul 11, 2012 at 16:05
\$\endgroup\$
2
\$\begingroup\$
  1. Start by creating a list of objects which contain the connection between a location type and the relevant controls. First create a new class to hold this information:

    public enum LocationType
    {
     Undefined = 0,
     Province = 1,
     District = 2,
     ...
    }
    public class LocationControl
    {
     public LocationType Type { get; set; }
     public TextBox TextBox { get; set; }
     public ComboBox ComboBox { get; set; }
    }
    private List<LocationControl> _myControls = new List<LocationControl>();
    
  2. Then, create type-control links:

    private void InitControls()
    {
     // a better approach might be to create all controls here,
     // but if you want to use the VS designer, this will do
     // (just call it inside OnLoad or something)
     _myControls.Add(new LocationControl()
     {
     Type = LocationType.Province,
     TextBox = txtProvince,
     ComboBox = ddlProvince
     });
     _myControls.Add(new LocationControl()
     {
     Type = LocationType.District,
     TextBox = txtDistrict,
     ComboBox = ddlDistrict
     });
     ...
    }
    
  3. Once you have this information, enabling the right control is trivial:

    private void EnableRelevantLocationControls(LocationType type)
    {
     // enable this input
     var activeLocation = _myControls.Find(c => c.Type == type);
     Enable(activeLocation);
     // disable all other inputs
     var inactiveLocations = _myControls.Where(c => c.Type != type);
     foreach (var location in inactiveLocations)
     Disable(location);
    }
    
answered Jul 12, 2012 at 14:31
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.