insert All Listbox items into a List - c#

Are there a way to export the items from an Listbox into a List in c#?
i have Listbox1 that Contain some string items and I have This list:
public List<String> MyList = new List<String>();
how to add this items to Mylist?

You could do a casting like this:
public List<String> MyList = listBox1.Items.Cast<string>().ToList();
This way, to change the ListBox ObjectCollection to list of string.

You can use AddRange method:
If you are sure that all elements in Listbox1 are strings:
MyList.AddRange(ListBox1.Items.Cast<string>());
if not:
MyList.AddRange(ListBox1.Items.OfType<string>());

Related

C# ListView to List<string>

I have a ListView with string items, and i am trying to get these items and store them in List container.
I need the reverse operation of this:
List<string> myList = new List<string> { "Item1", "item2", "item3"};
resultsList.ItemsSource = myList;
So my Question is How to Parse ListView with Items to List?
You need to cast it to IEnumerable<string>
List<string> myList = ((IEnumerable<string>)resultsList.ItemsSource).ToList();
I think you can cast and use ToList to create a list:
myList = resultsList.ItemsSource.Cast<string>().ToList();

c# - get multiple listbox values and turn into comma separated string winforms

I want to get all the selected list items in my listbox and put their values into a comma separated string. I populate the listbox using a List containing my custom object TestSubject which contains a TestSubjectID and TestSubjectName:
var objTestSubjectList = new List<TestSubject>();
//code that adds items to List
I then map the Listbox datasource to this list and set the DisplayMember, ValueMember correspondingly:
lbTestSubjects.DataSource = objTestSubjectList;
lbTestSubjects.DisplayMember = "TestSubjectName";
lbTestSubjects.ValueMember = "TestSubjectID";
When the multiple items in the listbox have been selected, I want to grab the values for each item (the TestSubjectID) and place them in a comma separated string. For example, if 3 items are selected with TestSubjectIDs of 10 20 and 30 I want to loop through and grab the values and put them in a string like so: "10,20,30". I am using windows forms. How would I accomplish this?
You need to loop over the SelectedIndices collection, retrieve the TestSubject object stored in the Item corresponding to the index selected and then add its TestSubjectID value to a list of integers. Finally string.Join will create the string for you.
List<int> ids = new List<int>();
foreach(int x in lbTestSubjects.SelectedIndices)
{
TestSubject t = lbTestSubjects.Items[x] as TestSubject ;
ids.Add(t.TestSubjectID);
}
string result = string.Join(",", ids);
Console.WriteLine(result);
This code could also be reduced a lot using LINQ
var ri = lbTestSubjects.SelectedIndices
.OfType<int>()
.Select(i => ((TestSubject)lbTestSubjects.Items[i]).TestSubjectID);
string result = string.Join(",", ri);

Top Item On Combo Box

I have windows forms project and List of type DateTime which I want to bind in combobox. I want on top of the combo box to have text All. How to do that ?
List<DateTime> dueDates = manager.GetUniqueDueDates();
cbDates.DataSource = dueDates;
For example
All
1/1/2001
1/1/2002
1/1/2003
You can use the Insert method of the items property to add the extra item.
cbDates.Items.Insert(0, "All");
This way your datasource does not need to be a list of string.
Update
As mentioned by #Hassan Nisar in the comments, it won't work if you bind the datasource, but you can add items using a loop(refer to #Hassan Nisar's answer for an example).
After binding with List<DateTime> you cannot insert item.
Argument exception will be raised Items collection cannot be modified when the DataSource property is set.
Skip binding with data source and add items by iterating through list:
List<DateTime> dueDates = manager.GetUniqueDueDates();
//cbDates.DataSource = dueDates;
foreach (var date in dueDates)
cbDates.Items.Add(date)
cbDates.Items.Insert(0, "All");
you need to get the list of dueDtes as string because "All" is string we cant add in Datetime list.
List<string> dueDates = manager.GetUniqueDueDates();
in GetUniqueDueDates function you need to add "All"
public List<string> GetUniqueDueDates()
{
List<string> uniqueDate=new List<string>();
uniqueDate.add("All");
//Rest of your code
}
Since your list is of type DateTime , you should first make it to List to make it work when you add "All"
List<string> dueDates = new List<string>();
dueDates.Add("All");
dueDates.Add(new DateTime(2001,03,01).ToString());
dueDates.Add(new DateTime(2002, 04, 01).ToString());
dueDates.Add(new DateTime(2003, 05, 01).ToString());
cbDates.DataSource = dueDates;
Try to sort all items after adding another 1 since its all dateTime after All .
Something like:
e.g: a was suppose to be your dataSource:
comboBox1.Items.AddRange(a.OrderBy(c => c).ToArray());
and property to true before anything else:
comboBox1.Sorted = true;

Populating listview with List<List<string>>

I'm coding in C# andI have a listview control with few columns, and I'm wondering, how can I add items to it from List<List<string>>. I am able to add items with
var item3 = new ListViewItem(new[] { table[0][0], table[0][1], table[0][2], table[0][3], table[0][5], table[0][7], table[0][8] });
but that doesn't seem right to me, and neither it would work because the amount of Lists is random.
You can use List<T>.ToArray to convert the list into an array which you then pass to the constructor of ListViewItem. As you are only interested in the first subitem of your table, you can just do it like this:
var item3 = new ListViewItem(table[0].ToArray());
You can use the following code to fill all the List<List<string>> into your ListView:
listView1.Items.AddRange(table.Select(list=>new ListViewItem(list.ToArray())))
.ToArray();
probably you mean like this ?
sample data
item1
List<string> list1 = new List<string>();
list1.Add("item-1.1");
list1.Add("item-1.2");
item2
List<string> list2 = new List<string>();
list2.Add("item-2.1");
list2.Add("item-2.2");
allitem
List<List<string>> listAll = new List<List<string>>();
listAll.Add(list1);
listAll.Add(list2);
string value
string sResult = string.Join(", ", from list in listAll from item in list select item);
list value
List<string> lResult = new List<string>(from list in listAll from item in list select item);
binding
lv.DataSource = lResult;
lv.DataBind();
result
item-1.1 item-1.2 item-2.1 item-2.2

How to select all items in a Listbox and concatenate them in ASP.NET C# Webform?

Right now I have
String myString = listbox1.Text.ToString();
However this only returns only the 1st item, even if I hit ctrl and select all of them.
Thanks for any help
Using an extension method, you can do this:
public static class Extensions
{
public static IEnumerable<ListItem> GetSelectedItems(this ListItemCollection items)
{
return items.OfType<ListItem>().Where(item => item.Selected);
}
}
Usage:
var selected = listbox1.Items.GetSelectedItems();
Now you can take the IEnumerable<ListItem> and convert that to a string array, then finally make it into a single string separated by semicolons, like this:
// Create list to hold the text of each list item
var selectedItemsList = new List<string>();
// Create list to hold the text of each list item
var selectedItemsList = selected.Select(listItem => listItem.Text).ToList();
// Build a string separated by comma
string selectedItemsSeparatedByComma = String.Join(",",
selectedItemsList.ToArray());
You are right, WebForms ListBox doesn't have the SelectedItems property. However, you can do
listBox.Items.OfType<ListItem>().Where(i => i.Selected);
That will give you the items you are looking for.
If you can't use LINQ, just do a foreach over listBox.Items, and do whatever you want when the item is Selected.

Categories

Resources