using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CheckedListBoxControl
{
	public partial class Form1 : Form
	{
		private List<Country> dataSource;

		public Form1()
		{
			InitializeComponent();

			InitInstance();
		}

		private void InitInstance()
		{
			dataSource = new List<Country>();
			dataSource.Add(new Country() { Name = "한국" });
			dataSource.Add(new Country() { Name = "영국" });
			dataSource.Add(new Country() { Name = "일본" });
			dataSource.Add(new Country() { Name = "중국" });
			dataSource.Add(new Country() { Name = "인도" });
			dataSource.Add(new Country() { Name = "프랑스" });
			dataSource.Add(new Country() { Name = "독일" });
			dataSource.Add(new Country() { Name = "이탈리아" });

			ListBox casted = ((ListBox)checkedListBox);
			casted.DataSource = dataSource;
			casted.DisplayMember = "Name";
			casted.ValueMember = "IsChecked";

			for (int i = 0; i < checkedListBox.Items.Count; i++)
			{
				Country country = (Country)checkedListBox.Items[i];
				checkedListBox.SetItemChecked(i, country.IsChecked);
			}
		}

		private void checkedListBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			Country selected = checkedListBox.SelectedItem as Country;
			if (selected == null)
				return;

			Console.WriteLine($"selected: Name {selected.Name}, Value {selected.IsChecked}");
		}
	}

	public class Country
	{
		public bool IsChecked { get; set; } = true;
		public string Name { get; set; } = "Country";
	}
}
