첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170
using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Declaration
        ////////////////////////////////////////////////////////////////////////////////////////// Class

        /// <summary>
        /// 직원
        /// </summary>
        public class Employee
        {
            //////////////////////////////////////////////////////////////////////////////////////////////////// Property
            ////////////////////////////////////////////////////////////////////////////////////////// Public

            #region ID - ID

            /// <summary>
            /// ID
            /// </summary>
            public string ID { get; set; }

            #endregion

            #region 성명 - Name

            /// <summary>
            /// 성명
            /// </summary>
            public string Name { get; set; }

            #endregion

            #region 설명 - Description

            /// <summary>
            /// 설명
            /// </summary>
            public string Description { get; set; }

            #endregion
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 체크 리스트 박스
        /// </summary>
        private CheckedListBox checkedListBox;

        /// <summary>
        /// 시작 인덱스
        /// </summary>
        private int startIndex;

        /// <summary>
        /// 종료 인덱스
        /// </summary>
        private int endIndex;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.checkedListBox = new CheckedListBox();

            this.checkedListBox.Dock         = DockStyle.Fill;
            this.checkedListBox.CheckOnClick = true;

            this.checkedListBox.MouseDown += checkedListBox_MouseDown;
            this.checkedListBox.MouseUp   += checkedListBox_MouseUp;

            Controls.Add(this.checkedListBox);

            ListBox listBox = this.checkedListBox as ListBox;

            if(listBox != null)
            {
                listBox.DataSource    = GetCollection(); // DataSource 속성부터 설정해야 한다.
                listBox.DisplayMember = "Name";
                listBox.ValueMember   = "ID";
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 체크 리스트 박스 마우스 UP 처리하기 - checkedListBox_MouseUp(sender, e)

        /// <summary>
        /// 체크 리스트 박스 마우스 UP 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void checkedListBox_MouseUp(object sender, MouseEventArgs e)
        {
            Point mousePoint = this.checkedListBox.PointToClient(Cursor.Position);

            for(int i = 0; i < this.checkedListBox.Items.Count; i++)
            {
                Rectangle itemRectangle = this.checkedListBox.GetItemRectangle(i);

                if(itemRectangle.Contains(mousePoint))
                {
                    this.endIndex = i;

                    break;
                }
            }

            this.checkedListBox.BeginUpdate();

            if(this.startIndex > this.endIndex)
            {
                for(int i = this.endIndex + 1; i <= this.startIndex; i++)
                {
                    this.checkedListBox.SetItemChecked(i, !this.checkedListBox.GetItemChecked(i));
                }
            }
            else
            {
                for(int i = this.startIndex; i < this.endIndex; i++)
                {
                    this.checkedListBox.SetItemChecked(i, !this.checkedListBox.GetItemChecked(i));
                }
            }

            this.checkedListBox.EndUpdate();
        }

        #endregion

        #region 체크 리스트 박스 마우스 DOWN 처리하기 - checkedListBox_MouseDown(sender, e)

        /// <summary>
        /// 체크 리스트 박스 마우스 DOWN 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void checkedListBox_MouseDown(object sender, MouseEventArgs e)
        {
            Point mousePoint = this.checkedListBox.PointToClient(Cursor.Position);

            for(int i = 0; i < this.checkedListBox.Items.Count; i++)
            {
                Rectangle itemRectangle = this.checkedListBox.GetItemRectangle(i);

                if(itemRectangle.Contains(mousePoint))
                {
                    this.startIndex = i;

                    break;
                }
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 컬렉션 구하기 - GetCollection()

        /// <summary>
        /// 컬렉션 구하기
        /// </summary>
        /// <returns>컬렉션</returns>
        private ObservableCollection<Employee> GetCollection()
        {
            ObservableCollection<Employee> collection = new ObservableCollection<Employee>();

            for(int i = 0; i < 100; i++)
            {
                collection.Add(new Employee() { ID = (i + 1).ToString(), Name = "직원 " + (i + 1).ToString(), Description = "설명 " + (i + 1).ToString() });
            }

            return collection;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요