Combo Box and General List

I have a list

 List<string> strArraylist = new List<string>();

I want to add combined block values ​​to it .

+3
source share
1 answer

UPDATE . I just understand that you may have asked the opposite of what I have outlined below: how to add elements from ComboBoxin List<string>. If so, you can always do it like this:

List<string> strList = new List<string>();
strList.AddRange(cbx.Items.Cast<object>().Select(x => x.ToString()));

The extension method is used here:

public static class ControlHelper
{
    public static void Populate<T>(this ComboBox comboBox, IEnumerable<T> items)
    {
        try
        {
            comboBox.BeginUpdate();
            foreach (T item in items)
            {
                comboBox.Items.Add(item);
            }
        }
        finally
        {
            comboBox.EndUpdate();
        }
    }
}

This allows you to populate ComboBoxany general collection that may be listed. See how easy it is to call:

List<string> strList = new List<string> { "abc", "def", "ghi" };
cbx.Populate(strList);

, , ComboBox.Items ( object Items). Populate IEnumerable IEnumerable<T>.

+2

Source: https://habr.com/ru/post/1754788/


All Articles