==> Program.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MyWinForms
{
    static class Program
    {
        /// <summary>
        /// 해당 응용 프로그램의 주 진입점입니다.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Controls.FrmCheckedListBox());
        }
    }
}



==============================================================================================



==> MainForm.cs


        // 체크리스트 박스 관련
        private void miCheckedListBox_Click(object sender, EventArgs e)
        {
            // 생성과 동시에 오픈
            (new MyWinForms.Controls.FrmCheckedListBox()).Show();
        }


==============================================================================================



==> FrmCheckedListBox.cs



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

namespace MyWinForms.Controls
{
    public partial class FrmCheckedListBox : Form
    {
        public FrmCheckedListBox()
        {
            InitializeComponent();
        }

        private void FrmCheckedListBox_Load(object sender, EventArgs e)
        {
            string[] fav = {"C#", "ASP.NET", "WPF", "Silverlight" };

            for (int i = 0; i < fav.Length; i++)
            {
                // 짝수만 체크된 상태
                if (i % 2 != 0)
                {
                    lstFavorites.Items.Add(fav[i], true);
                }
                else
                {
                    lstFavorites.Items.Add(fav[i]);
                }
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            // Input
            List<Favorite> lst = new List<Favorite>();
            string msg = "";
            // Process
            for (int i = 0; i < lstFavorites.CheckedItems.Count; i++)
            {
                lst.Add(new Favorite()
                {
                    Name = lstFavorites.CheckedItems[i].ToString()
                });
                msg += lstFavorites.CheckedItems[i].ToString();
            }
            // Output
            ctlSelectedList.DataSource = lst; // 배열, 컬렉션, 리스트를 출력할 수 있다.
            MessageBox.Show(msg);
        }
    }

    public class Favorite
    {
        public string Name { get; set; }
    }
}





<  실행결과 >






--> '선택'버튼을 누르면 '체크리스트박스'에 체크된 항목이 'MassageBox'와 'DataGridView'에 출력됨.





Posted by holland14
: