---
title: "How to do this in c#?"  
description: "How to do this in c#?"  
author: "Pravesh Singh"  
published: 2014-12-09  
updated: 2014-12-10  
canonical: https://www.mindstick.com/forum/12776/how-to-do-this-in-c-sharp  
category: ".net"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to do this in c#?

I have a List of MyClass and in the main [page](https://www.mindstick.com/articles/13031/why-to-make-a-wikipedia-page) I have 10 [controls](https://www.mindstick.com/articles/66/how-to-create-controls-at-run-time-in-csharp-dot-net) which will display the information about that list of [Items](https://www.mindstick.com/forum/33812/getting-number-of-items-selected-in-uicollectionview-in-ios). What I want is to [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) the [count](https://www.mindstick.com/forum/157774/selecting-count-with-distinct) of the items in the List and then make the excess controls invisible. [Now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now) I'm using this [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) but is there an easier way of doing this?

```
 if (myList.Count > 0) {      Control1.MyClassInfo = myList[0];      if (myList.Count > 1)      {           Control2.MyClassInfo = myList[1];           if (myList.Count > 2)           {                // and like that till 10           }           else           {                Control3.Visible = false;                Control4.Visible = false;                // till 10           }       }       else       {           Control2.Visible = false;           Control3.Visible = false;           // till 10       }  }  else  {       Control1.Visible = false;       Control2.Visible = false;       Control3.Visible = false;       // and then till 10  }
```

## Replies

### Reply by Anonymous User

You could create a list of your controls

List<Control> MyControls = new List<Control>{Control1, Control2,..,Control10};

and then

```
foreach(var C in MyControls)    C.Visible=false;for(int i=0; i<myList.Count; i++)    C[i].Visible=true;
```

### Reply by Anonymous User

Well, just add your controls in a list (ordered).

Something like that

var controlList = new List<Control>{ Control1, Control2, Control3 /*etc*/};

var count = myList.Count;

for (var i = 0; i < controlList.Count; i++) {

controlList[i].Visible = count > i;

}


---

Original Source: https://www.mindstick.com/forum/12776/how-to-do-this-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
