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

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【OpenCV】SIFT原理与源码分析

    【OpenCV】SIFT原理与源码分析SIFT简介ScaleInvariantFeatureTransform,尺度不变特征变换匹配算法,是由DavidG.Lowe在1999年(《ObjectRecognitionfromLocalScale-InvariantFeatures》)提出的高效区域检测算法,在2004年(《DistinctiveImageFeaturesfromScale-Inva

    2022年6月21日
    57
  • jquery ajax 请求中多出现一次OPTIONS请求及其解决办法

    jquery ajax 请求中多出现一次OPTIONS请求及其解决办法

    2021年10月13日
    43
  • es6之数组的flat(),flatMap()「建议收藏」

    es6之数组的flat(),flatMap()「建议收藏」数组的成员有时还是数组,Array.prototype.flat()用于将嵌套的数组“拉平”,变成一维数组。该方法返回一个新数组,对原数据没有影响。[1,2,[3,4]].flat()//[1,2,3,4]上面代码中,原数组的成员里面有一个数组,flat()方法将子数组的成员取出来,添加在原来的位置。flat()默认只会“拉平”一层,如果想要“拉平”多层的嵌套数组,可以将f…

    2022年5月30日
    75
  • C语言基础知识入门(大全)「建议收藏」

    C语言基础知识入门(大全)「建议收藏」一.C语言入门C语言一经出现就以其功能丰富、表达能力强、灵活方便、应用面广等特点迅速在全世界普及和推广。C语言不但执行效率高而且可移植性好,可以用来开发应用软件、驱动、操作系统等。C语言也是其它众多高级语言的鼻祖语言,所以说学习C语言是进入编程世界的必修课!更多详细进阶教程等你领取!可以关注公众号“C和C加加”回复“ZXC”即可免费获取!二.C语言的具体结构简单来说,一个C程序就是由若干头文件和函数组成。 #include<stdio.h>就是一条预处理命..

    2022年6月7日
    25
  • emwin实体按键_qt指示灯控件

    emwin实体按键_qt指示灯控件分享一个emWin软键盘控件[复制链接]本帖最后由glcd于2016-6-2716:30编辑花了两天时间做了个emWin软键盘控件,并命名为ButtonSKB控件:image001.png(20.29KB,下载次数:0)2016-6-2716:19上传前言:(1)ButtonSKB已经是1个控件,即可以像使用Button控件一样使用ButtonSKB。(2)ButtonSKB…

    2022年10月14日
    0
  • 虚拟机与宿主机网络配置——可互通可上网「建议收藏」

    虚拟机与宿主机网络配置——可互通可上网「建议收藏」     为了学习和使用Linux,多数人选择了使用虚拟机的方式来安装Linux系统。这样我们就可以在windows系统中安装Linux系统了,其中windows机器系统本身我们称作宿主机,安装的虚拟机系统我们简称虚拟机。     由于虚拟机提供的几种网络方式,要么是虚拟机可以ping通宿主机反之不行,要么是虚拟机之间可以互通但宿主机不能访问虚拟机,所以在实际应用中多数被两者间的网络互通和是否…

    2022年8月20日
    11

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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