How to get CheckedListBox selected values

A CheckedListBox is a list control with multiple checkboxes. This allows the user to check multiple boxes at once.

How can I can get all the values they selected? By looping through the CheckedListBox.CheckedItems collection. See the UI and Code examples below.

UI

Code

using System; using System.Collections.Generic; using System.Windows.Forms; namespace CheckedListBoxSelector { public partial class frmProgrammingLangs : Form { public frmProgrammingLangs() { InitializeComponent(); } private void btnSubmit_Click(object sender, EventArgs e) { var selectedLangs = new List<string>(); foreach(var lang in clbLanguages.CheckedItems) { selectedLangs.Add(lang.ToString()); } lblResult.Text = $"You selected: {string.Join(", ", selectedLangs)}"; } } }
Code language: C# (cs)

Leave a Comment