当前位置: 首页>>代码示例>>C#>>正文


C# ListView.Sorting属性代码示例

本文整理汇总了C#中System.Windows.Forms.ListView.Sorting属性的典型用法代码示例。如果您正苦于以下问题:C# ListView.Sorting属性的具体用法?C# ListView.Sorting怎么用?C# ListView.Sorting使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在System.Windows.Forms.ListView的用法示例。


在下文中一共展示了ListView.Sorting属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateMyListView

private void CreateMyListView()
{
    // Create a new ListView control.
    ListView listView1 = new ListView();
    listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));

    // Set the view to show details.
    listView1.View = View.Details;
    // Allow the user to edit item text.
    listView1.LabelEdit = true;
    // Allow the user to rearrange columns.
    listView1.AllowColumnReorder = true;
    // Display check boxes.
    listView1.CheckBoxes = true;
    // Select the item and subitems when selection is made.
    listView1.FullRowSelect = true;
    // Display grid lines.
    listView1.GridLines = true;
    // Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending;
                
    // Create three items and three sets of subitems for each item.
    ListViewItem item1 = new ListViewItem("item1",0);
    // Place a check mark next to the item.
    item1.Checked = true;
    item1.SubItems.Add("1");
    item1.SubItems.Add("2");
    item1.SubItems.Add("3");
    ListViewItem item2 = new ListViewItem("item2",1);
    item2.SubItems.Add("4");
    item2.SubItems.Add("5");
    item2.SubItems.Add("6");
    ListViewItem item3 = new ListViewItem("item3",0);
    // Place a check mark next to the item.
    item3.Checked = true;
    item3.SubItems.Add("7");
    item3.SubItems.Add("8");
    item3.SubItems.Add("9");

    // Create columns for the items and subitems.
    // Width of -2 indicates auto-size.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

    //Add the items to the ListView.
    listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});

    // Create two ImageList objects.
    ImageList imageListSmall = new ImageList();
    ImageList imageListLarge = new ImageList();

    // Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));

    //Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge;
    listView1.SmallImageList = imageListSmall;

    // Add the ListView to the control collection.
    this.Controls.Add(listView1);
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:66,代码来源:ListView.Sorting

示例2: Form1

//引入命名空间
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.ListView listView1;
        private System.Windows.Forms.ColumnHeader lvcState;
        private System.Windows.Forms.ColumnHeader lvcCapital;

        public Form1()
        {
            InitializeComponent();
            listView1.Items.Add (new ListViewItem (new string[]{"a","d"}));
            listView1.Items.Add (new ListViewItem (new string[]{"b","c"}));
            listView1.Items.Add (new ListViewItem (new string[]{"c","b"}));
            listView1.Items.Add (new ListViewItem (new string[]{"d","a"}));
            
            listView1.View =  View.Details;
        }
        private void InitializeComponent()
        {
            this.listView1 = new System.Windows.Forms.ListView();
            this.lvcState = new System.Windows.Forms.ColumnHeader();
            this.lvcCapital = new System.Windows.Forms.ColumnHeader();
            this.SuspendLayout();

            this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                                                                                        this.lvcState,
                                                                                        this.lvcCapital});
            this.listView1.FullRowSelect = true;
            this.listView1.Location = new System.Drawing.Point(0, 32);
            this.listView1.MultiSelect = false;
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(292, 160);
            this.listView1.Sorting = System.Windows.Forms.SortOrder.Descending;
            this.listView1.TabIndex = 3;
            this.listView1.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.listView1_OnColumnClick);
            this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_OnSelectedIndexChanged);
            // 
            // lvcState
            // 
            this.lvcState.Text = "State Name";
            this.lvcState.Width = 120;
            // 
            // lvcCapital
            // 
            this.lvcCapital.Text = "Capital City";
            this.lvcCapital.Width = 120;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.AddRange(new System.Windows.Forms.Control[] { this.listView1,
                                                                       });
            this.MinimumSize = new System.Drawing.Size(300, 300);
            this.Name = "Form1";
            this.Text = "The State Flags";
            this.ResumeLayout(false);

        }
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void listView1_OnColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            ListViewSorter Sorter = new ListViewSorter();
            listView1.ListViewItemSorter = Sorter;
            if (!(listView1.ListViewItemSorter is ListViewSorter))
                return;
            Sorter = (ListViewSorter) listView1.ListViewItemSorter;

            if (Sorter.LastSort == e.Column)
            {
                if (listView1.Sorting == SortOrder.Ascending)
                    listView1.Sorting = SortOrder.Descending;
                else
                    listView1.Sorting = SortOrder.Ascending;
            }
            else{
                listView1.Sorting = SortOrder.Descending;
            }
            Sorter.ByColumn = e.Column;

            listView1.Sort ();
        }

        private void listView1_OnSelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            string State = listView1.SelectedItems[0].SubItems[0].Text;
            
            string Capital = listView1.SelectedItems[0].SubItems[1].Text;
            if (State.Length > 0)
            {
                Console.WriteLine(State + "   " + Capital);
            }
        }
    }
    
    public class ListViewSorter : System.Collections.IComparer
    {
        public int Compare (object o1, object o2)
        {
            if (!(o1 is ListViewItem))
                return (0);
            if (!(o2 is ListViewItem))
                return (0);

            ListViewItem lvi1 = (ListViewItem) o2;
            string str1 = lvi1.SubItems[ByColumn].Text;
            ListViewItem lvi2 = (ListViewItem) o1;
            string str2 = lvi2.SubItems[ByColumn].Text;

            int result;
            if (lvi1.ListView.Sorting == SortOrder.Ascending)
                result = String.Compare (str1, str2);
            else
                result = String.Compare (str2, str1);

            LastSort = ByColumn;

            return (result);
        }


        public int ByColumn
        {
            get {return Column;}
            set {Column = value;}
        }
        int Column = 0;

        public int LastSort
        {
            get {return LastColumn;}
            set {LastColumn = value;}
        }
        int LastColumn = 0;
    }
开发者ID:C#程序员,项目名称:System.Windows.Forms,代码行数:151,代码来源:ListView.Sorting


注:本文中的System.Windows.Forms.ListView.Sorting属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。