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 SortListViewColumns
{
    public partial class frm75SortListViewColumns : Form
    {
        public frm75SortListViewColumns()
        {
            InitializeComponent();
        }

        private void frm75SortListViewColumns_Load(object sender, EventArgs e)
        {
            // Initialize the ListView.
            booksListView.SmallImageList = smallImageList;
            booksListView.LargeImageList = largeImageList;

            // Make the column headers.
            booksListView.MakeColumnHeaders(
                "Title", 230, HorizontalAlignment.Left,
                "URL", 220, HorizontalAlignment.Left,
                "ISBN", 130, HorizontalAlignment.Left,
                "Pages", 50, HorizontalAlignment.Right,
                "Year", 60, HorizontalAlignment.Right);

            // Add data rows.
            booksListView.AddRow(0, "Interview Puzzles Dissected", "http://www.csharphelper.com/puzzles.htm", "978-1-5395-0488-7", "300", "2016");
            booksListView.AddRow(1, "C# 24-Hour Trainer, 2nd Edition", "http://www.wrox.com/WileyCDA/WroxTitle/C-24-Hour-Trainer-2nd-Edition.productCd-1119065666.html", "978-1-119-06566-1", "600", "2015");
            booksListView.AddRow(2, "Beginning Software Engineering", "http://www.wrox.com/WileyCDA/WroxTitle/Beginning-Software-Engineering.productCd-1118969146.html", "978-1-118-96914-4", "480", "2015");
            booksListView.AddRow(3, "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");
            booksListView.AddRow(4, "Essential Algorithms: A Practical Approach to Computer Algorithms", "http://www.csharphelper.com/algorithms.html", "978-1-118-61210-1", "624", "2013");
        }

        private void largeIconsMenuItem_Click(object sender, EventArgs e)
        {
            booksListView.View = View.LargeIcon;
            CheckMenuItem(viewMenu, sender as ToolStripMenuItem);
        }

        private void smallIconsMenuItem_Click(object sender, EventArgs e)
        {
            booksListView.View = View.SmallIcon;
            CheckMenuItem(viewMenu, sender as ToolStripMenuItem);
        }

        private void listMenuItem_Click(object sender, EventArgs e)
        {
            booksListView.View = View.List;
            CheckMenuItem(viewMenu, sender as ToolStripMenuItem);
        }

        private void tileMenuItem_Click(object sender, EventArgs e)
        {
            booksListView.View = View.Tile;
            CheckMenuItem(viewMenu, sender as ToolStripMenuItem);
        }

        private void detailsMenuItem_Click(object sender, EventArgs e)
        {
            booksListView.View = View.Details;
            CheckMenuItem(viewMenu, sender as ToolStripMenuItem);
        }

        // Uncheck all menu items in this menu except checkedItem.
        private void CheckMenuItem(ToolStripMenuItem mnu, ToolStripMenuItem checkedItem)
        {
            // Uncheck all of the menu items.
            foreach (ToolStripItem item in mnu.DropDownItems)
            {
                if (item is ToolStripMenuItem)
                {
                    ToolStripMenuItem menuItem = item as ToolStripMenuItem;
                    menuItem.Checked = false;
                }
            }

            // Check the one that should be checked.
            checkedItem.Checked = true;
        }

        // The current sorting column.
        private ColumnHeader SortingColumn = null;

        // Sort on this column.
        private void booksListView_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            // Get the new sorting column.
            ColumnHeader newColumn = booksListView.Columns[e.Column];

            // Figure out the new sorting order.
            SortOrder sortOrder;
            if (SortingColumn == null)
            {
                // New column. Sort ascending.
                sortOrder = SortOrder.Ascending;
            }
            else
            {
                // See if this is the same column.
                if (newColumn == SortingColumn)
                {
                    // Same column. Switch the sort order.
                    if (SortingColumn.Text.StartsWith("> "))
                        sortOrder = SortOrder.Descending;
                    else
                        sortOrder = SortOrder.Ascending;
                }
                else
                {
                    // New column. Sort ascending.
                    sortOrder = SortOrder.Ascending;
                }

                // Remove the old sort indicator.
                SortingColumn.Text = SortingColumn.Text.Substring(2);
            }

            // Display the new sort order.
            SortingColumn = newColumn;
            if (sortOrder == SortOrder.Ascending)
                SortingColumn.Text = "> " + SortingColumn.Text;
            else
                SortingColumn.Text = "< " + SortingColumn.Text;

            // Create a comparer.
            booksListView.ListViewItemSorter =
                new ListViewComparer(e.Column, sortOrder);

            // Sort.
            booksListView.Sort();
        }


        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frm75SortListViewColumns));
            this.booksListView = new System.Windows.Forms.ListView();
            this.largeImageList = new System.Windows.Forms.ImageList(this.components);
            this.smallImageList = new System.Windows.Forms.ImageList(this.components);
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.viewMenu = new System.Windows.Forms.ToolStripMenuItem();
            this.largeIconsMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.smallIconsMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.listMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.tileMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.detailsMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // booksListView
            // 
            this.booksListView.AllowColumnReorder = true;
            this.booksListView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.booksListView.Location = new System.Drawing.Point(12, 27);
            this.booksListView.Name = "booksListView";
            this.booksListView.Size = new System.Drawing.Size(700, 187);
            this.booksListView.TabIndex = 17;
            this.booksListView.UseCompatibleStateImageBehavior = false;
            this.booksListView.View = System.Windows.Forms.View.Details;
            this.booksListView.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.booksListView_ColumnClick);
            // 
            // largeImageList
            // 
            this.largeImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("largeImageList.ImageStream")));
            this.largeImageList.TransparentColor = System.Drawing.Color.Transparent;
            this.largeImageList.Images.SetKeyName(0, "interview_puzzles_80_100.png");
            this.largeImageList.Images.SetKeyName(1, "csharp24hr_2e_80x100.jpg");
            this.largeImageList.Images.SetKeyName(2, "sw_eng_80x100.png");
            this.largeImageList.Images.SetKeyName(3, "csharp_prog_ref_80x100.png");
            this.largeImageList.Images.SetKeyName(4, "essential_algs_80x100.jpg");
            // 
            // smallImageList
            // 
            this.smallImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("smallImageList.ImageStream")));
            this.smallImageList.TransparentColor = System.Drawing.Color.Transparent;
            this.smallImageList.Images.SetKeyName(0, "book 31x32.png");
            this.smallImageList.Images.SetKeyName(1, "book 31x32.png");
            this.smallImageList.Images.SetKeyName(2, "book 31x32.png");
            this.smallImageList.Images.SetKeyName(3, "book 31x32.png");
            this.smallImageList.Images.SetKeyName(4, "book 31x32.png");
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.viewMenu});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(724, 24);
            this.menuStrip1.TabIndex = 18;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // viewMenu
            // 
            this.viewMenu.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.largeIconsMenuItem,
            this.smallIconsMenuItem,
            this.listMenuItem,
            this.tileMenuItem,
            this.detailsMenuItem});
            this.viewMenu.Name = "viewMenu";
            this.viewMenu.Size = new System.Drawing.Size(44, 20);
            this.viewMenu.Text = "&View";
            // 
            // largeIconsMenuItem
            // 
            this.largeIconsMenuItem.Name = "largeIconsMenuItem";
            this.largeIconsMenuItem.Size = new System.Drawing.Size(152, 22);
            this.largeIconsMenuItem.Text = "Large Icons";
            this.largeIconsMenuItem.Click += new System.EventHandler(this.largeIconsMenuItem_Click);
            // 
            // smallIconsMenuItem
            // 
            this.smallIconsMenuItem.Name = "smallIconsMenuItem";
            this.smallIconsMenuItem.Size = new System.Drawing.Size(152, 22);
            this.smallIconsMenuItem.Text = "Small Icons";
            this.smallIconsMenuItem.Click += new System.EventHandler(this.smallIconsMenuItem_Click);
            // 
            // listMenuItem
            // 
            this.listMenuItem.Name = "listMenuItem";
            this.listMenuItem.Size = new System.Drawing.Size(152, 22);
            this.listMenuItem.Text = "List";
            this.listMenuItem.Click += new System.EventHandler(this.listMenuItem_Click);
            // 
            // tileMenuItem
            // 
            this.tileMenuItem.Name = "tileMenuItem";
            this.tileMenuItem.Size = new System.Drawing.Size(152, 22);
            this.tileMenuItem.Text = "Tile";
            this.tileMenuItem.Click += new System.EventHandler(this.tileMenuItem_Click);
            // 
            // detailsMenuItem
            // 
            this.detailsMenuItem.Checked = true;
            this.detailsMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;
            this.detailsMenuItem.Name = "detailsMenuItem";
            this.detailsMenuItem.Size = new System.Drawing.Size(152, 22);
            this.detailsMenuItem.Text = "Details";
            this.detailsMenuItem.Click += new System.EventHandler(this.detailsMenuItem_Click);
            // 
            // frm75SortListViewColumns
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(724, 226);
            this.Controls.Add(this.booksListView);
            this.Controls.Add(this.menuStrip1);
            this.Name = "frm75SortListViewColumns";
            this.Text = "SortListViewColumns";
            this.Load += new System.EventHandler(this.frm75SortListViewColumns_Load);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.ListView booksListView;
        internal System.Windows.Forms.ImageList largeImageList;
        internal System.Windows.Forms.ImageList smallImageList;
        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem viewMenu;
        private System.Windows.Forms.ToolStripMenuItem largeIconsMenuItem;
        private System.Windows.Forms.ToolStripMenuItem smallIconsMenuItem;
        private System.Windows.Forms.ToolStripMenuItem listMenuItem;
        private System.Windows.Forms.ToolStripMenuItem tileMenuItem;
        private System.Windows.Forms.ToolStripMenuItem detailsMenuItem;
    }
}