Click here to Skip to main content
15,174,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a "2 dimensional" list, and a button in which I add items to the list and try to get the items and add them to a listbox. This is the problem, when adding the items of the list to the listbox, I want to add different strings/texts depending on which List<string> it is, based on a certain condition. I've tried using LINQ. So For example:
List<List<string>> getallwords;
Main mainlib; //class object

private void btnAddToList_Click(object sender, EventArgs e)
   if(textboxCount.Text == "3")
      mainlib.AddtoList(tbox1.Text, tbox2.Text, tbox3.Text); //method from class that adds three words to list
      getallwords = mainlib.GetWordList() //method to get items in list
      var 3wordsquery = getallwords.Where(item => item.Count == 3)

      foreach(List<string> item in 3wordsquery)
         listbox1.Items.Add("The houses are 3 in number");

   if(textboxCount.Text == "2")
      mainlib.AddtoList(tbox1.Text, tbox2.Text); //method to add just two words to list
      getallwords = mainlib.GetWordList() //method to get items in list
      var 2wordsquery = getallwords.Where(item => item.Count == 2)
      foreach(List<string> item in 2wordsquery)
         listbox1.Items.Add("The house are 2 in number");

But as the code shows, whenever the items are being added to the listbox, it returns either where item count is 2 or item count is 3. How can I make all items to be returned but still add different texts to the listbox?
Updated 6-Nov-15 5:33am
Maciej Los 6-Nov-15 10:18am
"I have a "2 dimensional" list: " List<List<string>> getallwords;
No, you don't! You have a list (of list (of strings)).
BillWoodruff 6-Nov-15 10:31am
No point in trying to help you with this if your code won't compile:

if(textboxCount.Text == 3) // going to throw an error here
Member 11971544 6-Nov-15 10:33am
BillWoodruff 6-Nov-15 10:34am
please clarify what "depending on which List it is" means.

you can improve your question, imho, by showing the code for: mainlib.AddtoList, and mainlib.GetWordList

why is 'mainlib never instantiated: is it a static class ?
Krunal Rohit 6-Nov-15 11:37am
There's no 2 dimensional list.


I guess you want to get the count of houses in for each list element. You're able to achieve that this way:

List<List<string>> getallwords = new List<List<string>>(){
    new List<string>{"A", "B", "C", "D"},
    new List<string>{"E", "F", "G"},
    new List<string>{"H", "I"},
    new List<string>{"J"}};

var result = getallwords
    .Select(grp=> new
        Message = string.Format("The count of houses in a group: {0}", grp.Key)

The count of houses in a group: 4
The count of houses in a group: 3
The count of houses in a group: 2
The count of houses in a group: 1
BillWoodruff 6-Nov-15 12:05pm
Maciej Los 6-Nov-15 12:06pm
Thank you, Bill.
I'm going to assume the key issue here is getting the TextBoxes that have text content passed to the 'mainlib.AddtoList method which I assume has an input parameter like this:

mainlib.AddtoList(params string[] args)

Assuming you have an Array of the TextBoxes used for input named 'TbxAry:
private TextBoxEx[] TbxAry;

// in Form_Load or other code:
TbxAry = new TextBoxEx[]
    textBox1, textBox2, textBox3, textBox4 // and so on ...

// requires System.Linq
private void SomeButton_Click(object sender, EventArgs e)
        .Where(tbx => (!string.IsNullOrEmpty(tbx.Text)))
        .Select(tbx => tbx.Text).ToArray()
I would eliminate a separate TextBox to hold a 'Count, and handle that some other way. You might consider using a Dictionary like this:

Dictionary<int, List<List<string>>> NToStrings;

which would be updated before you call mainlib.AddToList...

That way, you'd never have use Linq and pull out the Lists that have the same number of items. You'd just do this:
List<List<string>> results;
if(NToStrings.TryParse(3, out results)) 
   // you have a list of all lists with 3 elements in 'results ... do whatever
   // no results ... do whatever
Maciej Los 6-Nov-15 11:46am
Another good shot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900