A CheckedListBox is a list control with multiple checkboxes. This allows the user to check multiple boxes at once. You can also programmatically check items in the CheckedListBox and remove them.
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
Here’s the form with a CheckedListBox with some selected items:
Code
To loop through the items that have been checked, use a foreach loop on CheckedListBox.CheckedItems. Here’s an example of looping over the checked items and adding them to a list:
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)