Sorting Listbox Items by DateTime values - c#

Is there any easy way to sort listbox items by DateTime values?
In my listbox1 I have items formatted like this: "2013.01.08 19:29:52" so it's just someDateTimeValue.ToString()

if you have 2 ListBoxes you could do something like the following
ArrayList arList = new ArrayList();
foreach (object obj in listBox1.Items)
{
arList.Add(obj);
}
arList.Sort();
listBox2.Items.Clear();
foreach(object obj in arList)
{
listBox2.Items.Add(obj);
}

Try this
List<ListItem> myList = new List<ListItem>(ListBox1.Items.Cast<ListItem>());
myList = myList.OrderByDescending(li => li.Value).ToList<ListItem>();
ListBox1.Items.Clear();
ListBox1.Items.AddRange(myList.ToArray<ListItem>());

try to sort a list of dates and after that put in the listbox.
dateList.Sort();
var items = new SelectList(dateList);

Related

Return ComboBox From Function?

The below code has a list and loops through each string item in that list and adds it to a ComboBox. This functions correctly, but I am curious if there is a possible way to pass a string list and ComboBox into a function and return the ComboBox with each item in the string list being added.
Example: gets a string list, then adds each string item to the list. This is great if there's one ComboBox; but if there are 3 or more, to avoid code repetition, passing in a list and ComboBox would save code.
List<string> myList = getList();
foreach (string listItem in myList)
{
myComboBox.Items.Add(listItem);
}
you can make method like
private void FillCombo(ComboBox myComboBox, List<string> list);
{
foreach (string listItem in myList)
{
myComboBox.Items.Add(listItem);
}
//alternatively, you can add it like fubo suggested in comment
//myComboBox.Items.AddRange(myList.ToArray());
}
and call it from somewhere in code
List<string> myList = getList();
FillCombo(this.comboBox1, myList);
FillCombo(this.comboBox2, myList);
// etc...

How to convert string list to observable collection in windows phone

Hi all i have a list string like
List<string> numbers=new List<string>();
now i want to convert it on observable collection i have converted it successfully like
ObservableCollection<string> myCollection = new ObservableCollection<string>(numbers);
but when i am deleting a item from list box like
myCollection.Remove(listBox1.SelectedItem.ToString());
listBox1.ItemsSource = myCollection;
the above code is deleted all the item in list box but i want to delete specific item in list box.
Try this
Initialize collection and lsitbox
List<string> numbers=new List<string>();
//numbers.Add("test"); //populate list
ObservableCollection<string> myCollection = new ObservableCollection<string>(numbers);
listBox1.ItemsSource = myCollection;
now use the below code to remove the selected item from the list
var selectedItem =listbox1.SelectedItem as string;
if(myCollection.Contains(selectedItem)
{
myCollection.Remove(selectedItem);
}
Instead of binding ObservableCollection you can directly bind List<string> to your listBox1.ItemsSource. See this example to bind List<string>
windows phone data binding listpicker to List of Strings
To remove items from list try this
listBox1.Items.Remove(listBox1.SelectedItem);
Answer_:
numbers.RemoveAt(listBox1.SelectedIndex);
listBox1.ItemsSource = null;
listBox1.ItemsSource = numbers;

how to Copy listView selected items into an array

I'm trying to get listView items into an array..
in listBox
listBox1.SelectedItems
would do the trick.
But it didn't work in listView...
any Ideas???
Do like this,
var myList = new List<string>();
foreach(ListViewItem Item in ListView.SelectedItems)
{
myList.add(Item.Text.ToString());
}
var myArray = myList.ToArray();

List for both checkedlist box and combobox

I want to add all selected items of checkedlistbox and combobox to a list and use that list in a for each loop in c#
I tried this
List<String> list=new List<String>();
if (rbtnMultipleScenario.Checked == true)
{
foreach ( CheckedListBox str in clbScenario.SelectedItems)
{
lstitems.Add(str);
}
}
By using String, I am not able to add all the selected items of Checkedlistbox.
Which type of list I have to use?
List<string> list=new List<string>();
if (rbtnMultipleScenario.Checked == true)
{
foreach ( string str in clbScenario.SelectedItems)
{
lstitems.Add(str);
}
}
This assumes that SelectedItems contains a collection of strings (which by your exception it does)

How to add to a list multiple selected values of a ListBox ? C#, ASP.NET web site

I'm working on one of my first projects. I have a listbox where I select multiple values and I would like to add each selection (selectedItem.Text) to a list of strings.
so far what I was working on is something like ..
selectedItem = new List<string>();
var value = lstpdfList.SelectedItem.Text;
for (int i = 0; i < lstpdfList.SelectedValue.Count(); i++)
{
selectedItem.Add(value);
}
I would really appreciate any advice.
Iterate each item from ListBox.Items collection
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
{
selectedItem.Add(item.Text); // selectedImte.Add(item.Value);
}
}
There is SelectedItems property of ListBox, try to iterate throught it. For example, if there is strings in your ListBox, then your code might look like this:
selectedItem = new List<string>();
foreach (string value in lstpdfList.SelectedValues)
selectedItem.Add(value);
You can just cast them to strings:
var selectedItems = listBox1.SelectedItems
.Cast<string>()
.ToList();
If you have populated your ListBox with something other than just strings, just cast to whichever type you need, like so:
var selectedItems = listBox1.SelectedItems
.Cast<WhateverYourTypeIs>()
.Select(item => item.ToString())
.ToList();

Categories