Hello everyone, I have a button handler in WinForms, when I press a button, I get and CheckedListBox contents of the collection in CheckedListBox . The collection itself consists of two text fields, this is the name of the rubric and its URL. Further on the form I choose the positions I need. And when I press the second button, I want to form a parameterized List of the selected items. But I only form the list with those Items that have been selected, but these are only names, but I don’t know how to get the URLs of the selected positions, here’s the code.

Here is the handler of the first button that gets the list of sections.

 private void button1_Click(object sender, EventArgs e) { string urlsRubr = "https://www.rabota66.ru/resume"; // тут получаю саму коллекцию все ок. var listt= pars.GetRubrik(urlsRubr).ToArray(); // заполняю сам список на форме. Вывожу собственно имена рубрик. // У x есть еще один параметр это x.UrlRubrik тут храниться сами урлы. checkedListBox1.Items.AddRange(listt.Select(x => x.NameRubrik).ToArray()); } 

Here is the handler of the second button, when clicked, the selected positions are stored in the same parameterized List . But the problem is that I will add only the names of the headings, and how can I also hook the urls themselves. Can I somehow get the name and invisible url on the form, so that together we can put it all into the collection with the selected items?

 private void button2_Click(object sender, EventArgs e) { List<Rubriks> Chek=new List<Rubriks>(); foreach (var item in checkedListBox1.CheckedItems) { Chek.Add((Rubriks) item); } } 

    2 answers 2

    You can use data binding features.

    Suppose there is:

    Class heading:

     public class Rubric { public string Name { get; set; } public string Url { get; set; } } 

    Seeking control:

     CheckedListBox checkedListBox; 

    Filled Collection:

     var rubrics = new List<Rubric> { new Rubric { Name = "NameA", Url = "UrlA" }, new Rubric { Name = "NameB", Url = "UrlB" } }; 

    At CheckedListBox itself CheckedListBox some properties are hidden, so we bring it to the ListBox :

     var listBox = (ListBox)checkedListBox; listBox.DataSource = rubrics; // привязка данных listBox.DisplayMember = nameof(Rubric.Name); // какое свойство будет выводиться 

    Further in the button-click we get the selected items:

     var checkedRubrics = checkedListBox.CheckedItems.OfType<Rubric>(); 

    You can add ToList() .


    nameof - returns the name of a variable, type, member. Without using it, one would write:

     listBox.DisplayMember = "Name"; 

    But if a typo is made, then it will be known only in runtime.

    OfType - returns items of the specified type from the collection. The CheckedItems collection contains elements of type object . Here we bring them to the type we need.

    • cool, but I was looking for DisplayMember in CheckedListBox and did not find it - tym32167
    • Cool, very cool. I did not know that it is possible to bring almost identical controllers to each other, and explain the meaning of the nameof and OfType - Vladimr Vladimirovoch
    • one
      @VladimrVladimirovoch - added the answer. - Alexander Petrov

    If the class override ToString, it is also possible to send this class entirely to the list of checkboxes. for example

     public class MyItem { public string Name {get;private set;} public int Id {get;private set;} public MyItem(string name, int id) { Name = name; Id = id; } public override string ToString() { return Name; } } 

    form example

     public class MyForm : Form { public MyForm() { var cb = new CheckedListBox(); cb.Items.AddRange(Enumerable.Range(0, 10) .Select(x => new MyItem($"My name is {x}", x)).ToArray()); var bt = new Button() {Text = "press me"}; bt.Click += (sender, args) => { var sb = new StringBuilder(); foreach (var item in cb.CheckedItems.OfType<MyItem>()) sb.AppendLine($"checked: id:{item.Id} with name:{item.Name}"); MessageBox.Show(sb.ToString()); }; this.Controls.Add(bt); this.Controls.Add(cb); bt.Left = 120; } } 

    Result

    enter image description here

    • Thanks for the reply - Vladimr Vladimirovoch