첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ ListView 클래스 : 그룹 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Windows.Forms;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : Form

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            #region 리스트 박스를 설정한다.

 

            ListViewGroup cSharpListViewGroup = new ListViewGroup("C# Books");

            ListViewGroup vbListViewGroup     = new ListViewGroup("Visual Basic Books");

 

            this.listView.Groups.Add(cSharpListViewGroup);

            this.listView.Groups.Add(vbListViewGroup);

 

            // C# Books.

            this.listView.Items.Add

            (

                new ListViewItem

                (

                    new string[]

                    {

                        "C# 5.0 Programmer's Reference",

                        "http://www.wrox.com/WileyCDA/WroxTitle/C-5-0-Programmer-s-Reference.productCd-1118847288.html",

                        "978-1-118-84728-2",

                        "960",

                        "2014"

                    },

                    cSharpListViewGroup

                )

            );

 

            this.listView.Items.Add

            (

                new ListViewItem

                (

                    new string[]

                    {

                        "MCSD Certification Toolkit (Exam 70-483): Programming in C#",

                        "http://www.wrox.com/WileyCDA/WroxTitle/C-5-0-Programmer-s-Reference.productCd-1118847288.html",

                        "978-1-118-61209-5",

                        "648",

                        "2013"

                    },

                    cSharpListViewGroup

                )

            );

 

            // Visual Basic Books

            this.listView.Items.Add

            (

                new ListViewItem

                (

                    new string[]

                    {

                        "Visual Basic 2012 Programmer's Reference",

                        "http://www.wrox.com/WileyCDA/WroxTitle/Visual-Basic-2012-Programmer-s-Reference.productCd-1118314077.html",

                        "978-1-118-31407-4",

                        "840",

                        "2012"

                    },

                    vbListViewGroup

                )

            );

 

            // Other Books

            this.listView.Items.Add

            (

                new ListViewItem

                (

                    new string[]

                    {

                        "Essential Algorithms: A Practical Approach to Computer Algorithms ",

                        "http://www.wiley.com/WileyCDA/WileyTitle/productCd-1118612108.html",

                        "978-1-118-61210-1",

                        "624",

                        "2013"

                    }

                )

            );

 

            this.listView.Items.Add

            (

                new ListViewItem

                (

                    new string[]

                    {

                        "Beginning Database Design Solutions",

                        "http://www.vb-helper.com/db_design.htm",

                        "978-0-470-38549-4",

                        "552",

                        "2008"

                    }

                )

            );

 

            this.listView.Items.Add

            (

                new ListViewItem

                (

                    new string[]

                    {

                        "Start Here! Fundamentals of Microsoft .NET Programming",

                        "http://www.amazon.com/Start-Here-Fundamentals-Microsoft-Programming/dp/0735661685",

                        "978-0-735-66168-4",

                        "264",

                        "2011"

                    }

                )

            );

 

            #endregion

            #region 이벤트를 설정한다.

 

            this.detailMenuItem.Click    += detailMenuItem_Click;

            this.largeIconMenuItem.Click += largeIconMenuItem_Click;

            this.listMenuItem.Click      += listMenuItem_Click;

            this.smallIconMenuItem.Click += smallIconMenuItem_Click;

            this.tileMenuItem.Click      += tileMenuItem_Click;

 

            #endregion

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 상세 메뉴 항목 클릭시 처리하기 - detailMenuItem_Click(sender, e)

 

        /// <summary>

        /// 상세 메뉴 항목 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void detailMenuItem_Click(object sender, EventArgs e)

        {

            this.listView.View = View.Details;

 

            CheckMenuItem(this.viewMenuItem, this.detailMenuItem);

        }

 

        #endregion

        #region 큰 아이콘 메뉴 항목 클릭시 처리하기 - largeIconMenuItem_Click(sender, e)

 

        /// <summary>

        /// 큰 아이콘 메뉴 항목 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void largeIconMenuItem_Click(object sender, EventArgs e)

        {

            this.listView.View = View.LargeIcon;

 

            CheckMenuItem(this.viewMenuItem, this.largeIconMenuItem);

        }

 

        #endregion

        #region 목록 메뉴 항목 클릭시 처리하기 - listMenuItem_Click(sender, e)

 

        /// <summary>

        /// 목록 메뉴 항목 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void listMenuItem_Click(object sender, EventArgs e)

        {

            this.listView.View = View.List;

 

            CheckMenuItem(this.viewMenuItem, this.listMenuItem);

        }

 

        #endregion

        #region 작은 아이콘 메뉴 항목 클릭시 처리하기 - smallIconMenuItem_Click(sender, e)

 

        /// <summary>

        /// 작은 아이콘 메뉴 항목 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void smallIconMenuItem_Click(object sender, EventArgs e)

        {

            this.listView.View = View.SmallIcon;

 

            CheckMenuItem(this.viewMenuItem, this.smallIconMenuItem);

        }

 

        #endregion

        #region 타일 메뉴 항목 클릭시 처리하기 - tileMenuItem_Click(sender, e)

 

        /// <summary>

        /// 타일 메뉴 항목 클릭시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void tileMenuItem_Click(object sender, EventArgs e)

        {

            this.listView.View = View.Tile;

 

            CheckMenuItem(this.viewMenuItem, this.tileMenuItem);

        }

 

        #endregion

 

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

 

        #region 메뉴 항목 체크하기 - CheckMenuItem(parentMenuItem, checkChildMenuItem)

 

        /// <summary>

        /// 메뉴 항목 체크하기

        /// </summary>

        /// <param name="parentMenuItem">부모 메뉴 항목</param>

        /// <param name="checkChildMenuItem">체크 자식 메뉴 항목</param>

        private void CheckMenuItem(ToolStripMenuItem parentMenuItem, ToolStripMenuItem checkChildMenuItem)

        {

            foreach(ToolStripItem toolStripItem in parentMenuItem.DropDownItems)

            {

                if(toolStripItem is ToolStripMenuItem)

                {

                    ToolStripMenuItem childMenuItem = toolStripItem as ToolStripMenuItem;

 

                    childMenuItem.Checked = false;

                }

            }

 

            checkChildMenuItem.Checked = true;

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요