N
N
nika_fox2019-12-18 23:05:46
C++ / C#
nika_fox, 2019-12-18 23:05:46

How to write from each line in a combobox with several words, separated by commas, into a new one, where is a new word on each line?

5dfa86584b6b7161508005.jpeg
I have words separated by commas in each line in the combobox, how can I add one word at a time to the new combobox so that they do not repeat?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Pogrebnyak, 2019-12-19
@nika_fox

c - combobox.

var items = new List<string>();
foreach (var item in c.Items.Cast<string>())
{
       foreach (var split in item.Split(','))
       {
             items.Add(split);
       }
}
c.Items.Clear();
c.Items.AddRange(items.Distinct().ToArray());

A double loop can also be written like this:
c.Items.Cast<string>().ToList().ForEach(item => item.Split(',').ToList().ForEach(split => items.Add(split)));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question