ListView灵活的用法

以下是示例的效果图:WinForm的ListView控件是可以分组显示的,还可排序。可以把ListView的View属性设置为Details完整项目请到下面网址查找下载http://hovertre

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

以下是示例的效果图:
ListView灵活的用法

 

WinForm的ListView控件是可以分组显示的,还可排序。

可以把ListView的View属性设置为Details

完整项目请到下面网址查找下载

http://hovertree.com/hovertreescj/

或者:
http://hovertree.com/h/bjaf/scjyuanma.htm

具体实现在项目 HoverTreeWindowsFormsDemo 中,位于HtDemo文件夹下。

 

以下是代码:

/*
 http://hovertree.com/hovertreescj/
本示例展示如何使用ListView分组显示数据。
h : hovertree
 */
using System;
using System.Collections;
using System.Windows.Forms;

namespace HoverTreeWindowsFormsDemo.HtFormSet
{
    public partial class Form_ListView : Form
    {
        public Form_ListView()
        {
            InitializeComponent();
        }

        // Determine whether Windows XP or a later
        // operating system is present.
        private bool _isRunningXPOrLater =
            OSFeature.Feature.IsPresent(OSFeature.Themes);

        // Declare a Hashtable array in which to store the groups.
        private Hashtable[] _groupTables;

        // Declare a variable to store the current grouping column.
        int _groupColumn = 0;

        private void Form_ListView_Load(object sender, EventArgs e)
        {

            ColumnHeader h_columnHeader0 = new ColumnHeader();
            h_columnHeader0.Text = "Title";
            // columnHeader0.Width = -1;
            h_columnHeader0.Width = 200;
            ColumnHeader h_columnHeader1 = new ColumnHeader();
            h_columnHeader1.Text = "Info";
            //columnHeader1.Width = -1;
            h_columnHeader1.Width = 150;

            ColumnHeader h_columnHeader2 = new ColumnHeader();
            h_columnHeader2.Text = "Year";
            // columnHeader2.Width = -1;
            h_columnHeader2.Width = 100;
            // Add the column headers to listView_HoverTree.
            listView_HoverTree.Columns.AddRange(new ColumnHeader[]
                {h_columnHeader0, h_columnHeader1, h_columnHeader2});

            // Add a handler for the ColumnClick event.
            listView_HoverTree.ColumnClick +=
                new ColumnClickEventHandler(listView_HoverTree_ColumnClick);

            // Create items and add them to listView_HoverTree.
            ListViewItem item0 = new ListViewItem(new string[]
                {"HoverTreeSCJ",
            "Hewenqi",
            "2016"});
            ListViewItem item1 = new ListViewItem(new string[]
                {"Keleyi: jQuery and HTML5",
            "柯乐义",
            "2012"});
            ListViewItem item2 = new ListViewItem(new string[]
                {"hwq2.com",
            "A Good Site",
            "2015"});
            ListViewItem item3 = new ListViewItem(new string[]
                {"何问起收藏夹",
            "HT",
            "2012"});
            ListViewItem item4 = new ListViewItem(new string[]
                {"HoverClock",
            "HTML5 Clock",
            "2016"});
            ListViewItem item5 = new ListViewItem(new string[]
                {"EasySector",
            "HTML5 canvas",
            "2016"});
            listView_HoverTree.Items.AddRange(
                new ListViewItem[] { item0, item1, item2, item3, item4, item5 });

            if (_isRunningXPOrLater)
            {
                // Create the groupsTable array and populate it with one 
                // hash table for each column.
                _groupTables = new Hashtable[listView_HoverTree.Columns.Count];
                for (int column = 0; column < listView_HoverTree.Columns.Count; column++)
                {
                    // Create a hash table containing all the groups 
                    // needed for a single column.
                    _groupTables[column] = CreateGroupsTable(column);
                    //groupTables[column]
                }

                // Start with the groups created for the Title column.
                SetGroups(0);
            }

            // Initialize the form.
            this.Controls.Add(listView_HoverTree);
            this.Size = new System.Drawing.Size(550, 330);
            this.Text = "ListView Groups Example_何问起";
        }

        // Groups the items using the groups created for the clicked 
        // column.
        private void listView_HoverTree_ColumnClick(
            object sender, ColumnClickEventArgs e)
        {
            // Set the sort order to ascending when changing
            // column groups; otherwise, reverse the sort order.
            if (listView_HoverTree.Sorting == SortOrder.Descending ||
                (_isRunningXPOrLater && (e.Column != _groupColumn)))
            {
                listView_HoverTree.Sorting = SortOrder.Ascending;
            }
            else
            {
                listView_HoverTree.Sorting = SortOrder.Descending;
            }
            _groupColumn = e.Column;

            // Set the groups to those created for the clicked column.
            if (_isRunningXPOrLater)
            {
                SetGroups(e.Column);
            }
        }

        // Sets listView_HoverTree to the groups created for the specified column.
        private void SetGroups(int column)
        {
            // Remove the current groups.
            listView_HoverTree.Groups.Clear();

            // Retrieve the hash table corresponding to the column.
            Hashtable groups = (Hashtable)_groupTables[column];

            // Copy the groups for the column to an array.
            ListViewGroup[] h_groupsArray = new ListViewGroup[groups.Count];
            groups.Values.CopyTo(h_groupsArray, 0);

            // Sort the groups and add them to listView_HoverTree.
            Array.Sort(h_groupsArray, new ListViewGroupSorter(listView_HoverTree.Sorting));
            listView_HoverTree.Groups.AddRange(h_groupsArray);

            // Iterate through the items in listView_HoverTree, assigning each 
            // one to the appropriate group.
            foreach (ListViewItem item in listView_HoverTree.Items)
            {
                // Retrieve the subitem text corresponding to the column.
                string h_subItemText = item.SubItems[column].Text;

                // For the Title column, use only the first letter.
                if (column == 0)
                {
                    h_subItemText = h_subItemText.Substring(0, 1);
                }

                // Assign the item to the matching group.
                item.Group = (ListViewGroup)groups[h_subItemText];
            }
        }

        // Creates a Hashtable object with one entry for each unique
        // subitem value (or initial letter for the parent item)
        // in the specified column.
        private Hashtable CreateGroupsTable(int column)
        {
            // Create a Hashtable object.
            Hashtable h_groups = new Hashtable();

            // Iterate through the items in listView_HoverTree.
            foreach (ListViewItem item in listView_HoverTree.Items)
            {
                // Retrieve the text value for the column.
                string h_subItemText = item.SubItems[column].Text;

                // Use the initial letter instead if it is the first column.
                if (column == 0)
                {
                    h_subItemText = h_subItemText.Substring(0, 1);
                }

                // If the groups table does not already contain a group
                // for the subItemText value, add a new group using the 
                // subItemText value for the group header and Hashtable key.
                if (!h_groups.Contains(h_subItemText))
                {
                    h_groups.Add(h_subItemText, new ListViewGroup(h_subItemText,
                        HorizontalAlignment.Left));
                }
            }

            // Return the Hashtable object.
            return h_groups;
        }

        // Sorts ListViewGroup objects by header value.
        private class ListViewGroupSorter : IComparer
        {
            private SortOrder h_order;

            // Stores the sort order.
            public ListViewGroupSorter(SortOrder theOrder)
            {
                h_order = theOrder;
            }

            // Compares the groups by header value, using the saved sort
            // order to return the correct value.
            public int Compare(object x, object y)
            {
                int result = String.Compare(
                    ((ListViewGroup)x).Header,
                    ((ListViewGroup)y).Header
                );
                if (h_order == SortOrder.Ascending)
                {
                    return result;
                }
                else
                {
                    return -result;
                }
            }
        }

    }
}

转自:http://hovertree.com/h/bjaf/jynj6isd.htm

推荐:http://www.cnblogs.com/roucheng/p/csgeshi.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/120305.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • vue项目结构解读[通俗易懂]

    1、整体结构解读2、分布解读

  • HTML和CSS面试题及答案总结一

    HTML和CSS面试题及答案总结一对于html的语义化标签的理解,结构化标签的理解,同时写出简洁的html结构,如何进行SEO优化?答:对于html的语义化标签,用正确的标签做正确的事情。html语义化,让页面的内容结构化,便于对浏览器和搜索引擎的解析,在没有css样式的情况下,以文档的形式同样易于阅读,符合文档语义的标签。标签本身所代表的语义,每一个标签所带有的语义,根据语义去使用标签,依赖标记确定权重,同时也可以提高SE…

  • Builder 构造器模式[通俗易懂]

    Builder 构造器模式[通俗易懂]Builder 构造器模式动机模式定义实例结构要点总结笔记动机在软件系统中,有时候面临着”一个复杂对象“的创建过程,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将他们组合在一起的算法却通常相对稳定。如何应对这种变化?如何提供一种”封装机制“来隔离出”复杂对象的各个部分”的变化,从而保持系统中的“稳定构建算法”不随着需求改变而改变?模式定义将一个复杂对象的构建与其表示相分离,使得同样的构建过程(稳定)可以创建出不通的表示(变化)实例构

  • 黑盒测试的等价类划分法_黑盒测试等价类输出

    黑盒测试的等价类划分法_黑盒测试等价类输出1、等价类划分2、边界值分析

  • phpspreadsheet中文手册_php打开文件

    phpspreadsheet中文手册_php打开文件本文介绍PhpSpreadsheet读写excel文件的一些使用方法。

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号