How can I store the selected item(s) in a listbox to a new array? Like we select the items and then do the button's action, e.g.
string[] domains = new string[listBox1.Items.Count];
for (int i = 0; i < listBox1.Items.Count; i++)
{
domains[i] = listBox1.SelectedIndices[i].ToString();
}
Your code won't work because index i is not used to indicate SelectedIndices, but Items.
So update it:
string[] domains = new string[listBox1.SelectedIndices.Count];
for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
{
domains[i] = listBox.Items[listBox1.SelectedIndices[i]].ToString();
}
What I prefer:
List<string> domains = new List<string>();
for (int i = 0; i < listBox1.SelectedIndices.Count; i++)
{
domains.Add(listBox.Items[listBox1.SelectedIndices[i]].ToString());
}
What about this:
string[] domains = listBox1.SelectedItems.OfType<string>().ToArray();
Related
I want to create multiple lists from a given size. Imagining it could look something like this :
int Size = int.Parse(Console.ReadLine());
for (int i = 0; i < Size; i++)
{
List<string> ListName + i = new List<string>();
}
So for example if size = 5 I'd get 5 lists :
ListName0
ListName1
ListName2
ListName3
ListName4
Create a container for the lists outside your loop:
int Size = int.Parse(Console.ReadLine());
List<List<string>> listContainer = new List<List<string>>();
for (int i = 0; i < Size; i++)
{
listContainer.Add(new List<string>());
}
You can access them via the index of the container object. For example listContainer[0] would be the first list<string> in the container.
Here is an example of accessing one of the lists and then accessing a value from said list:
int Size = int.Parse(Console.ReadLine());
List<List<string>> listContainer = new List<List<string>>();
for (int i = 0; i < Size; i++)
{
listContainer.Add(new List<string>());
}
listContainer[0].Add("Hi");
Console.WriteLine(listContainer[0][0]);
the common way is to use a dictionary
var list = new Dictionary<string, List<string>>();
int size = int.Parse(Console.ReadLine());
for (int i = 0; i < size; i++)
list["Name"+i.ToString()] = new List<string>();
how to use
list["Name1"].Add( "hello world");
I want to fill in a 2Dlist, but start at the third position [2]. Is this somehow possible?
A short code for understanding what i mean:
List<List<string>> List2D = new List<List<string>>();
for (int i = 0; i < 5; i++)
{
List2D[2].Add("i")
}
I get the following error:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
EDIT: Any idea how to fill in a 4D list?
List<List<List<List<string>>>> List4D = new List<List<List<List<string>>>>();
for (int i = 0; i < List1.Count; i++)
{
List<List<List<string>>> List3D = new List<List<List<string>>>();
for (int j = 0; j < List2.Count; j++)
{
List<List<string>> List2D = new List<List<string>>();
for (int k = 0; k < List3.Count; k++)
{
List<string> Lijst1D = new List<string>();
List2D.Add(Lijst1D);
}
List3D.Add(List2D);
}
List4D.Add(List3D);
}
So later I can call: List4D[2][3][0].Add("test");
Since you just created your List2D and not added any nested list into it, you can't access its third element (there is nothing there).
You have to add some items first:
List<List<string>> List2D = new List<List<string>>();
List2D.Add(new List<string>());
List2D.Add(new List<string>());
List2D.Add(new List<string>());
for (int i=0; i<5; i++)
{
List2D[2].Add("i")
}
Update
Well, core idea of filling that list remains the same: if you want to access List4D[2][3][0] - first you need to fill all of lists in "path".
You can do it something like this:
List<List<List<List<string>>>> List4D = new List<List<List<List<string>>>>();
int i1 = 2, i2 = 3, i3 = 0;
for (int i = 0; i <= Math.Max(i1, 1); i++)
List4D.Add(new List<List<List<string>>>());
for (int i = 0; i <= Math.Max(i2, 1); i++)
List4D[i1].Add(new List<List<string>>());
for (int i = 0; i <= Math.Max(i3, 1); i++)
List4D[i1][i2].Add(new List<string>());
List4D[i1][i2][i3].Add("test");
Frankly, idea of 4D list looks a little bit "syntetic". In real application probably it is not the best data structure because of clumsy addressing.
I am trying to having a new list added on every for loop iteration. I have the following code:
for (int i = 0; i < torni.Length; i++)
{
List<string> torni+i = // STUCK HER
}
Listnames should be like torni0, torni1, torni2 ......
Would really appreciate your assistance. Thanks!!
One way of doing this that would be slightly different would be to make a list of lists and then each index would be a discrete list.
You can't dynamically generate variable names in c#
like this:
tornis = new List<List<String>>()
for (int i = 0; i < torni.Length; i++)
{
tornis.append(new List<String>())
}
Alternatively as DanH Points out a dictionary of lists
tornis = new Dictionary<String,List<String>()
for (int i = 0; i < torni.Length; i++)
{
tornis.add("torni" + i, new List<String>())
}
This will give you a dictionary with the keys of the convention you want and a list of lists.
If you need each list only for the duration of a single loop iteration, then you don't need different list names:
for (int i = 0; i < torni.Length; i++)
{
List<string> temporaryList = new List<string>();
// use the list here
}
I'm have list of result as
var attributeresult= " some list of items......";
Now I'm trying to loop form result and adding into IList.but I'm getting only lastly inserting values,but I want to get all the values.
IList<DynamicColumn> idynamicttableColumns = new List<DynamicColumn>();
DynamicColumn dynamictableColumns = new DynamicColumn();
for (int i = 0; i < attributeresult.Count(); i++)
{
dynamictableColumns.Name = attributeresult.ElementAt(i).AttributeName;
dynamictableColumns.Type = attributeresult.ElementAt(i).AttributeSqlType;
dynamictableColumns.IsNullable = false;
idynamicttableColumns.Add(dynamictableColumns);
}
I have to accomplish with for loop only not with for each loop.
Move DynamicColumn dynamictableColumns = new DynamicColumn(); into your loop:
IList<DynamicColumn> idynamicttableColumns = new List<DynamicColumn>();
int count = attributeresult.Count();
for (int i = 0; i < count; i++)
{
var item = attributeresult.ElementAt(i);
DynamicColumn dynamictableColumns = new DynamicColumn();
dynamictableColumns.Name = item .AttributeName;
dynamictableColumns.Type = item .AttributeSqlType;
dynamictableColumns.IsNullable = false;
idynamicttableColumns.Add(dynamictableColumns);
}
I want to create dynamically 10 Labels inside a for loop
string labelName;
for(int i = 0; i < 10; i++)
{
labeName = "Label" & i;
// Creata & Instanciate the label here, How ?
}
How would you create a bunch of objects which weren't UI elements? Use a collection:
List<Label> labels = new List<Label>();
for (int i = 0; i < 10; i++)
{
Label label = new Label();
// Set properties here
labels.Add(label);
}
You'll presumably want to add these labels to a form or page or whatever too...
List<string> labelName = new List<string>();
for(int i = 0; i < 10; i++)
{
labeName.Add(string.Concat("Label", i));
}