How to get the selected values ​​from a CheckedListBox in c#

Roberto Carlos Espinosa 25 Reputation points
2023-09-21T02:29:02.95+00:00

I come with a question since I have no idea how to capture the data stored in the following checkedlistbox, which in this example is made up of 6 options to be able to check, in reality there are like 20, each selection option is a field in a table that allows null values ​​or default the selected option.

Captura de pantalla 2023-09-19 212250

How could or how should I be able to capture this data to store it in a temporary variable and later in a method to be able to save the corresponding selection.

I thank you in advance for your help.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,801 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 44,516 Reputation points Microsoft Vendor
    2023-09-21T03:57:58.7366667+00:00

    Hi @Roberto Carlos Espinosa , Welcome to Microsoft Q&A,

    You can use foreach to iterate through all items in the checklistbox to get them.

    You can also traverse the checked items to get the checked items.

    Then you can save them where you want:

    Here's an example of mine:

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace _9_21_x
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //access all items
                List<string> values = new List<string>();
                foreach (var item in checkedListBox1.Items)
                {
                    values.Add(item.ToString());
                }
                Console.WriteLine("All items:");
                foreach (var item in values)
                {
                    Console.WriteLine(item);
                }
    
                //access all checked items
                List<string> checkedValues = new List<string>();
                foreach (var item in checkedListBox1.CheckedItems)
                {
                    checkedValues.Add(item.ToString());
                }
                Console.WriteLine("All checked items:");
                foreach (var item in checkedValues)
                {
                    Console.WriteLine(item);
                }
    
            }
        }
    }
    
    

    User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.