3

I have ten labels on a page. I want to make these invisible in a for loop on page load.

I have tried this (doesn't work):

    for (int i = 0; i < 10; i++)
    {
        my_lbl+i.Visible = false;
    }

Therefore, it should do:

my_lbl1.Visible = false;
my_lbl2.Visible = false;
my_lbl3.Visible = false;
my_lbl4.Visible = false;

etc...

Is there a way to do this?

3

6 Answers 6

7

Put all of the labels into a collection:

private List<Label> labels = new List<Label>{my_lbl1, my_lbl2, my_lbl3, my_lbl4};

Then you can iterate the whole collection:

foreach(var label in labels)
    label.Visible = false;
Sign up to request clarification or add additional context in comments.

Comments

4

Make a List of them;

List<Label> yourlabels = new List<Label>{my_lbl1, my_lbl2, my_lbl3...};

and use foreach loop making them visible.

foreach(var label in yourlabels)
{
   label.Visible = false;
}

I don't know if there is a better way but this way seems logical to me.

Comments

3

Putting the labels in a collection (as the previous answers have suggested) is a great solution. You can also retrieve the controls by their name using FindControl method of the Page.

for (int i = 0; i < 10; i++)
{
    this.FindControl("my_lbl" + i.ToString()).Visible = false;
}

3 Comments

Unfortunately I just get the error: Object reference not set to an instance of an object. when I try and use this code
The thing with FindControl is that you have to call it from the parent control. So in this particular snippet 'this' refers to the Page, so it is assumed the labels are directly contained in it. If, however, the labels are inside another control (like a Panel), then you need to call FindControl from that parent control.
No apologies required. We all make mistakes on our way to enlightenment :D
1

I guess you can utillize Page's FindControl method:

for (int i = 0; i < 10; i++)
{
    FindControl(string.Format("my_lbl{0}", i)).Visible = false;
}

But check the case if control is not found of course.

Comments

1

Or, you can put them into dictionary:

    Dictionary<string, Label> nameOfDict = new Dictionary<string, Label>();

    nameOfDict.Add("label1", label1);
    nameOfDict.Add("label2", label2);

For...

nameOfDict ["label" + incrementator].visible = false;

Or, create them dynamically into an array of labels.

Comments

0

If you're sure that, let's say, you want to uncheck all checkboxes in a groupbox, you can do this, too:

foreach (var item in groupBox1.Controls)
{
    if (item.GetType() == typeof(CheckBox))
    {
        ((CheckBox)item).Checked = true;
    }
}

with LINQ:

foreach (var item in groupBox1.Controls.Cast<object>().Where(item => item.GetType() == typeof(CheckBox)))
{
    ((CheckBox)item).Checked = true;
}

7 Comments

If you're going to use LINQ just use TypeOf; it makes the whole thing a lot easier. Note that this is only possible if there is a control whose children is exactly the Controls you need, if there are other labels you don't want to affect in the lowest parent that contains all of those you do want to affect, this isn't feasible.
@Servy Thank you for this additional information. Could you please specifiy where TypeOf cleans up the LINQ expression? And in the 2nd part of your info: Yes, it's important that you're sure that "all" controls (of type checkbox in this case) are the controls you want. For this reason I used the groupbox as container to clarify this.
Well, the whole query become groupBox1.Controls.OfType<Label>() rather than groupBox1.Controls.Cast<object>().Where(item => item.GetType() == typeof(Label)). It's a pretty obvious refactor given that you want to do exactly what it does, based on it's definition. And saying that you're putting all of the content in a group box doesn't change the fact that it may not be possible. It's not just about describing how; it's quite possible, even probable, that it can't be done in this specific case.
@Servy You're right - it is not possible if the labels are not child controls of a parent control. (But if you use forms and want to display labels, there is at least the form as parent control, even if no groupboxes or panels are used.) Do I miss another reason to do this (running through the controls-list of the parent) not?
There will be some parent common to all of these; it will be the page if nothing else. The question is, "Are there any other labels that are also children of that parent which shouldn't be processed?" Also, if you need to go up more than one level to find the common parent, you need to search recursively through the child nodes, not just to a depth of 1.
|

Your Answer

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.