views:

111

answers:

5

How do I do this in a loop.

protected void ddlTool_SelectedIndexChanged(object sender, EventArgs e)
{

    lblTool1.Visible = false;
    txtTool1.Visible = false;
    lblTool2.Visible = false;
    txtTool2.Visible = false;
    lblTool3.Visible = false;
    txtTool3.Visible = false;
    lblTool4.Visible = false;
    txtTool4.Visible = false;
    lblTool5.Visible = false;


    if (ddlTool.SelectedValue == "1")
    {
        lblTool1.Visible = true;
        txtTool1.Visible = true;
    }
    if (ddlTool.SelectedValue == "2")
    {
        lblTool1.Visible = true;
        txtTool1.Visible = true;
        lblTool2.Visible = true;
        txtTool2.Visible = true;
    }
    if (ddlTool.SelectedValue == "3")
    {
        lblTool1.Visible = true;
        txtTool1.Visible = true;
        lblTool2.Visible = true;
        txtTool2.Visible = true;
        lblTool3.Visible = true;
        txtTool3.Visible = true;
    }
    if (ddlTool.SelectedValue == "4")
    {
        lblTool1.Visible = true;
        txtTool1.Visible = true;
        lblTool2.Visible = true;
        txtTool2.Visible = true;
        lblTool3.Visible = true;
        txtTool3.Visible = true;
        lblTool4.Visible = true;
        txtTool4.Visible = true;
    }
+2  A: 

You can access a control by its name using

container.Controls["nameofcontrol"]

So technically, you could use this to lookup your control

(Untested code)

for(int index = 1; index <= Convert.ToInt32(ddlTool.SelectedValue); index++)
{
    this.Controls["lblTool" + index.ToString()].Visible = true;
    this.Controls["txtTool" + index.ToString()].Visible = true;
}
Pierre-Alain Vigeant
+1  A: 

Use a UserControl for each set of connected controls and then enable/disable the UserControl instead of all the component controls. This is classic, basic modularization of your user interface.

Note that this will still require a little "redundant" code because you're working with an unusual UI paradigm by enabling up-to the ddlTool's selected value of your control. E.g., create your user control that contains a single Label and TextBox. Call it LabeledTextBox or something similar. Then you'd create a collection of your labeled text boxes and enable them up to int.Parse(ddlTool.SelectedValue) - 1.

Greg D
A: 
foreach (Control ctrl in this.Controls)
{
    int index = (int)ctrl.Name.Substring(ctrl.Name.Length - 1);
    int maxIndex = (int)ddlTool.SelectedValue;
    ctrl.Visible = (index <= maxIndex);
}
MusiGenesis
+7  A: 

Instead of having a separate variable for each textbox and label, have a collection of them - whether that's a List<T> or an array or whatever.

Then you can do:

// Potentially use int.TryParse here instead
int visibleLabels = int.Parse(ddlTool.SelectedValue);
for (int i = 0; i < labels.Count; i++)
{
    labels[i].Visible = (i < visibleLabels);
    textBoxes[i].Visible = (i < visibleLabels);
}

(Alternatively use two loops, one to set some Visible properties to true, and one to set some to false.)

Jon Skeet
Since it's not obvious from the original description whether this is about Windows Forms or ASP.NET, I'll explain for both: make sure you initialize the array only once the controls are created (on Windows Forms, it's after the call to InitializeComponent(); on ASP.NET; it's after FrameworkInitialize(), so usually in the Load event). A better solution would be to use properties to initialize the arrays on first access only, potentially using .NET 4' new Lazy<T> class if you can.
Francis Gagné
Since control arrays aren't natively supported I thought this article about Creating Control Arrays in .NET would be a useful supplement: http://msdn.microsoft.com/en-us/library/aa289500%28VS.71%29.aspx
Sorax
A: 

Here is an example with no error checking, but should match your code functionality.

protected void ddlTool_SelectedIndexChanged(object sender, EventArgs e) 
{
  int selectedValue = int.Parse(ddlTool.SelectedValue.ToString());
  for (int i = 1; i <= 4; ++i)
  {
    bool state = i <= selectedValue;
    this.Controls["lblTool" + i.ToString()].Visible = state;
    this.Controls["txtTool" + i.ToString()].Visible = state;
  }
}
Chris Taylor