How can I copy a collection of items in a comboBox
to a StringCollection
in my C# application? I'm only interested in capturing the string text for each item in their respective order. I am trying to make a MRU file list that is saved between sessions, so I would like to copy comboBox.Items
to StringCollection Properties.Settings.Default.MostRecentlyUsedHexFiles
. Any thoughts or suggestions you may have would be appreciated. Thanks.
views:
91answers:
1You should be able to loop over the combobox.items and simply use stringcollection.Add() to add the string to the collection.
The tostring method will perform as described here:
Although the ComboBox is typically used to display text items, you can add any object to the ComboBox. Typically, the representation of an object in the ComboBox is the string returned by that object's ToString method. If you want to have a member of the object displayed instead, choose the member that will be displayed by setting the DisplayMember property to the name of the appropriate member. You can also choose a member of the object that will represent the value returned by the object by setting the ValueMember property. For more information, see ListControl.
So something like:
Foreach(object o in combobox.items)
{
//might need to access a datamember of the combobox's item if more complex solution is required, but this will probably do
stringcollection.Add(o.ToString);
}