listView介绍

 
 

C# ListView用法详解 很完整

 分类:
C#(30) 
一、ListView类

          1、常用的基本属性:

        (1)FullRowSelect:设置是否行选择模式。(默认为false) 提示:只有在Details视图该属性才有意义。

        (2) GridLines:设置行和列之间是否显示网格线。(默认为false)提示:只有在Details视图该属性才有意义。

        (3)AllowColumnReorder:设置是否可拖动列标头来对改变列的顺序。(默认为false)提示:只有在Details视图该属性才有意义。

        (4)View:获取或设置项在控件中的显示方式,包括Details、LargeIcon、List、SmallIcon、Tile(默认为 LargeIcon)

        (5)MultiSelect:设置是否可以选择多个项。(默认为false)

        (6)HeaderStyle:获取或设置列标头样式。

                  Clickable:列标头的作用类似于按钮,单击时可以执行操作(例如排序)。

                  NonClickable:列标头不响应鼠标单击。

                  None:不显示列标头。

        (7)LabelEdit:设置用户是否可以编辑控件中项的标签,对于Detail视图,只能编辑行第一列的内容。(默认为false)

        (8)CheckBoxes:设置控件中各项的旁边是否显示复选框。(默认为false)

        (9)LargeImageList:大图标集。提示:只在LargeIcon视图使用。

        (10)SmallImageList:小图标集。提示:只有在SmallIcon视图使用。

        (11)StateImageList:图像蒙板。这些图像蒙板可用作LargeImageList和SmallImageList图像的覆盖图,这些图像可用于指示项的应用程序定义的状态。(暂时不大懂)

        (12)SelectedItems:获取在控件中选定的项。

        (13)CheckedItems:获取控件中当前复选框选中的项。

        (14)Soritng:对列表视图的项进行排序。(默认为None)

                    Ascending:项按递增顺序排序。

                    Descending:项按递减顺序排序。

                    None:项未排序。

         (15)Scrollable:设置当没有足够空间来显示所有项时是否显示滚动条。(默认为true)

         (16)HoverSelection:设置当鼠标指针悬停于项上时是否自动选择项。(默认为false)

         (17)HotTracking:设置当鼠标指针经过项文本时,其外观是否变为超链接的形式。(默认为false)

         (18)HideSelection:设置选定项在控件没焦点时是否仍突出显示。(默认为false)

         (19)ShowGroups:设置是否以分组方式显示项。(默认为false);

         (20)Groups:设置分组的对象集合。

          (21)TopItem:获取或设置控件中的第一个可见项,可用于定位。(效果类似于EnsureVisible方法)

           2、常用方法:

          (1)BeginUpdate:避免在调用EndUpdate 方法之前描述控件。当插入大量数据时,可以有效地避免控件闪烁,并能大大提高速度。

          (2)EndUpdate:在BeginUpdate 方法挂起描述后,继续描述列表视图控件。(结束更新)

          (3)EnsureVisible:列表视图滚动定位到指定索引项的选项行。(效果类似于TopItem属性)

          (4)FindItemWithText:查找以给定文本值开头的第一个 ListViewItem。

           (5)FindNearestItem:按照指定的搜索方向,从给定点开始查找下一个项。提示:只有在LargeIcon或SmallIcon视图才能使用该方法。

            3、常用事件:

         (1)AfterLabelEdit:当用户编辑完项的标签时发生,需要LabelEdit属性为true。

         (2)BeforeLabelEdit:当用户开始编辑项的标签时发生。

         (3)ColumnClick:当用户在列表视图控件中单击列标头时发生。

   二、ListView的五种视图:

        1、LargeIcon:每个项都显示为一个最大化图标,在它的下面有一个标签。(效果见下图)

         2、SmallIcon:每个项都显示为一个小图标,在它的右边带一个标签。(效果见下图)

         3、List:每个项都显示为一个小图标,在它的右边带一个标签。各项排列在列中,没有列标头。(效果见下图)

         4、Details:可以显示任意的列,但只有第一列可以包含一个小图标和标签,其它的列项只能显示文字信息,有列表头。(效果见下图)

         5、Tile:每个项都显示为一个完整大小的图标,在它的右边带项标签和子项信息。(只有Windows XP 和 Windows Server 2003 系列支持)

        ①Details视图:

            this.listView1.SmallImageList = this.imageList1;  //将listView的图标集与imageList1绑定

         (1)列表头创建(记得,需要先创建列表头)       

[csharp]  view plain copy print ?
 
  1. ColumnHeader  ch= new ColumnHeader(); 
  2.  
  3. nbsp;ch.Text = "列标题1";   //设置列标题 
  4.  
  5. ch.Width = 120;    //设置列宽度 
  6.  
  7. ch.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式 
  8.  
  9. this.listView1.Columns.Add(ch);    //将列头添加到ListView控件。 
[csharp]  view plain  copy
 
 print?
  1. ColumnHeader  ch= new ColumnHeader();  
  2.   
  3. ch.Text = "列标题1";   //设置列标题  
  4.   
  5. ch.Width = 120;    //设置列宽度  
  6.   
  7. ch.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式  
  8.   
  9. this.listView1.Columns.Add(ch);    //将列头添加到ListView控件。  

                          或者

[csharp]  view plain copy print ?
 
  1. this.listView1.Columns.Add("列标题1", 120,  HorizontalAlignment.Left); //一步添加 
[csharp]  view plain  copy
 
 print?
  1. this.listView1.Columns.Add("列标题1", 120,  HorizontalAlignment.Left); //一步添加  
       (2)添加数据项       
[csharp]  view plain copy print ?
 
  1. this.listView1.BeginUpdate();   //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度 
  2.  
  3. for (int i = 0; i < 10; i++)   //添加10行数据 
  4.      ListViewItem lvi = new ListViewItem(); 
  5.  
  6.      lvi.ImageIndex = i;     //通过与imageList绑定,显示imageList中第i项图标 
  7.  
  8.      lvi.Text = "subitem" + i; 
  9.  
  10.      lvi.SubItems.Add("第2列,第"+i+"行"); 
  11.  
  12.      lvi.SubItems.Add("第3列,第"+i+"行"); 
  13.  
  14.      this.listView1.Items.Add(lvi); 
  15.  
  16. this.listView1.EndUpdate();  //结束数据处理,UI界面一次性绘制。 
[csharp]  view plain  copy
 
 print?
  1. this.listView1.BeginUpdate();   //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度  
  2.   
  3. for (int i = 0; i < 10; i++)   //添加10行数据  
  4. {  
  5.     ListViewItem lvi = new ListViewItem();  
  6.   
  7.     lvi.ImageIndex = i;     //通过与imageList绑定,显示imageList中第i项图标  
  8.   
  9.     lvi.Text = "subitem" + i;  
  10.   
  11.     lvi.SubItems.Add("第2列,第"+i+"行");  
  12.   
  13.     lvi.SubItems.Add("第3列,第"+i+"行");  
  14.   
  15.     this.listView1.Items.Add(lvi);  
  16. }  
  17.   
  18. this.listView1.EndUpdate();  //结束数据处理,UI界面一次性绘制。  
          (3)显示项
[csharp]  view plain copy print ?
 
  1. foreach (ListViewItem item in this.listView1.Items) 
  2.       for (int i = 0; i < item.SubItems.Count; i++) 
  3.       { 
  4.           MessageBox.Show(item.SubItems[i].Text); 
  5.       } 
[csharp]  view plain  copy
 
 print?
  1. foreach (ListViewItem item in this.listView1.Items)  
  2. {  
  3.       for (int i = 0; i < item.SubItems.Count; i++)  
  4.       {  
  5.           MessageBox.Show(item.SubItems[i].Text);  
  6.       }  
  7. }  
           (4)移除某项
[csharp]  view plain copy print ?
 
  1. foreach (ListViewItem lvi in listView1.SelectedItems)  //选中项遍历 
  2.       listView1.Items.RemoveAt(lvi.Index); // 按索引移除 
  3.       //listView1.Items.Remove(lvi);   //按项移除 
  4. }    
[csharp]  view plain  copy
 
 print?
  1. foreach (ListViewItem lvi in listView1.SelectedItems)  //选中项遍历  
  2. {  
  3.       listView1.Items.RemoveAt(lvi.Index); // 按索引移除  
  4.       //listView1.Items.Remove(lvi);   //按项移除  
  5. }     
          (5)行高设置(利用imageList实现)
[csharp]  view plain copy print ?
 
  1. ImageList imgList = new ImageList(); 
  2.  
  3. imgList.ImageSize = new Size(1, 20);// 设置行高 20 //分别是宽和高 
  4.  
  5. listView1.SmallImageList = imgList; //这里设置listView的SmallImageList ,用imgList将其撑大 
[csharp]  view plain  copy
 
 print?
  1. ImageList imgList = new ImageList();  
  2.   
  3. imgList.ImageSize = new Size(1, 20);// 设置行高 20 //分别是宽和高  
  4.   
  5. listView1.SmallImageList = imgList; //这里设置listView的SmallImageList ,用imgList将其撑大  
          (6)清空
[csharp]  view plain copy print ?
 
  1. this.listView1.Clear();  //从控件中移除所有项和列(包括列表头)。 
  2.  
  3. this.listView1.Items.Clear();  //只移除所有的项。 
[csharp]  view plain  copy
 
 print?
  1. this.listView1.Clear();  //从控件中移除所有项和列(包括列表头)。  
  2.   
  3. this.listView1.Items.Clear();  //只移除所有的项。  

 

运行效果:

        ②largeIcon视图:

[csharp]  view plain copy print ?
 
  1. this.listView1.View = View.LargeIcon; 
  2.  
  3. this.listView1.LargeImageList = this.imageList2; 
  4.  
  5. this.listView1.BeginUpdate(); 
  6.  
  7. for (int i = 0; i < 10; i++) 
  8.       ListViewItem lvi = new ListViewItem(); 
  9.  
  10.       lvi.ImageIndex = i; 
  11.  
  12.       lvi.Text = "item" + i; 
  13.  
  14.       this.listView1.Items.Add(lvi); 
  15.  
  16. this.listView1.EndUpdate(); 
[csharp]  view plain  copy
 
 print?
  1. this.listView1.View = View.LargeIcon;  
  2.   
  3. this.listView1.LargeImageList = this.imageList2;  
  4.   
  5. this.listView1.BeginUpdate();  
  6.   
  7.  for (int i = 0; i < 10; i++)  
  8.  {  
  9.       ListViewItem lvi = new ListViewItem();  
  10.   
  11.       lvi.ImageIndex = i;  
  12.   
  13.       lvi.Text = "item" + i;  
  14.   
  15.       this.listView1.Items.Add(lvi);  
  16.  }  
  17.   
  18.  this.listView1.EndUpdate();  
运行效果:

          ③SmallIcon视图:

[csharp]  view plain copy print ?
 
  1. this.listView1.View = View.SmallIcon; 
  2.  
  3. this.listView1.SmallImageList= this.imageList1; 
  4.  
  5. this.listView1.BeginUpdate(); 
  6.  
  7. for (int i = 0; i < 10; i++) 
  8.      ListViewItem lvi = new ListViewItem(); 
  9.  
  10.       lvi.ImageIndex = i; 
  11.  
  12.       lvi.Text = "item" + i; 
  13.  
  14.       this.listView1.Items.Add(lvi); 
  15.  
  16. this.listView1.EndUpdate(); 
[csharp]  view plain  copy
 
 print?
  1. this.listView1.View = View.SmallIcon;  
  2.   
  3. this.listView1.SmallImageList= this.imageList1;  
  4.   
  5. this.listView1.BeginUpdate();  
  6.   
  7. for (int i = 0; i < 10; i++)  
  8. {  
  9.      ListViewItem lvi = new ListViewItem();  
  10.   
  11.       lvi.ImageIndex = i;  
  12.   
  13.       lvi.Text = "item" + i;  
  14.   
  15.       this.listView1.Items.Add(lvi);  
  16. }  
  17.   
  18. this.listView1.EndUpdate();  
运行效果:

           ④List视图:   

[csharp]  view plain copy print ?
 
  1. this.listView1.View = View.List; 
  2.  
  3. this.listView1.SmallImageList= this.imageList1; 
  4.  
  5. this.listView1.BeginUpdate(); 
  6.  
  7. for (int i = 0; i < 10; i++) 
  8.      ListViewItem lvi = new ListViewItem(); 
  9.  
  10.      lvi.ImageIndex = i; 
  11.  
  12.      lvi.Text = "item" + i; 
  13.  
  14.      this.listView1.Items.Add(lvi); 
  15.  
  16. this.listView1.EndUpdate(); 
[csharp]  view plain  copy
 
 print?
  1. this.listView1.View = View.List;  
  2.   
  3. this.listView1.SmallImageList= this.imageList1;  
  4.   
  5. this.listView1.BeginUpdate();  
  6.   
  7. for (int i = 0; i < 10; i++)  
  8. {  
  9.      ListViewItem lvi = new ListViewItem();  
  10.   
  11.      lvi.ImageIndex = i;  
  12.   
  13.      lvi.Text = "item" + i;  
  14.   
  15.      this.listView1.Items.Add(lvi);  
  16. }  
  17.   
  18. this.listView1.EndUpdate();  
运行效果:

 

 

     三、其它应用

         1、分组:      

[csharp]  view plain copy print ?
 
  1. ListViewGroup man_lvg = new ListViewGroup();  //创建男生分组 
  2.  
  3. man_lvg.Header = "男生";  //设置组的标题。 
  4.  
  5. //man_lvg.Name = "man";   //设置组的名称。 
  6.  
  7. man_lvg.HeaderAlignment = HorizontalAlignment.Left;   //设置组标题文本的对齐方式。(默认为Left) 
  8.  
  9. ListViewGroup women_lvg = new ListViewGroup();  //创建女生分组 
  10.  
  11. women_lvg.Header = "女生"; 
  12.  
  13. //women_lvg.Name = "women"; 
  14.  
  15. women_lvg.HeaderAlignment = HorizontalAlignment.Center;   //组标题居中对齐 
  16.  
  17. this.listView1.Groups.Add(man_lvg);    //把男生分组添加到listview中 
  18.  
  19. this.listView1.Groups.Add(women_lvg);   //把男生分组添加到listview中 
  20.  
  21. this.listView1.ShowGroups = true;  //记得要设置ShowGroups属性为true(默认是false),否则显示不出分组 
  22.  
  23. for (int i = 0; i < 5; i++) 
  24.      ListViewItem lvi = new ListViewItem(); 
  25.  
  26.      lvi.ImageIndex = i; 
  27.  
  28.      lvi.Text = "item"+i; 
  29.  
  30.      lvi.ForeColor = Color.Blue;  //设置行颜色 
  31.  
  32.      lvi.SubItems.Add("第2列,第"+i+"行"); 
  33.  
  34.      lvi.SubItems.Add("第3列,第"+i+"行"); 
  35.  
  36.      man_lvg.Items.Add(lvi);   //分组添加子项 
  37.  
  38.      // 或 lvi.Group = man_lvg;  //分组添加子项 
  39.  
  40.      this.listView1.Items.Add(lvi); 
[csharp]  view plain  copy
 
 print?
  1.  ListViewGroup man_lvg = new ListViewGroup();  //创建男生分组  
  2.   
  3.  man_lvg.Header = "男生";  //设置组的标题。  
  4.   
  5.  //man_lvg.Name = "man";   //设置组的名称。  
  6.   
  7.  man_lvg.HeaderAlignment = HorizontalAlignment.Left;   //设置组标题文本的对齐方式。(默认为Left)  
  8.   
  9.  ListViewGroup women_lvg = new ListViewGroup();  //创建女生分组  
  10.   
  11.  women_lvg.Header = "女生";  
  12.   
  13.  //women_lvg.Name = "women";  
  14.   
  15.  women_lvg.HeaderAlignment = HorizontalAlignment.Center;   //组标题居中对齐  
  16.   
  17.  this.listView1.Groups.Add(man_lvg);    //把男生分组添加到listview中  
  18.   
  19.  this.listView1.Groups.Add(women_lvg);   //把男生分组添加到listview中  
  20.   
  21.  this.listView1.ShowGroups = true;  //记得要设置ShowGroups属性为true(默认是false),否则显示不出分组  
  22.   
  23.  for (int i = 0; i < 5; i++)  
  24.  {  
  25.      ListViewItem lvi = new ListViewItem();  
  26.   
  27.      lvi.ImageIndex = i;  
  28.   
  29.      lvi.Text = "item"+i;  
  30.   
  31.      lvi.ForeColor = Color.Blue;  //设置行颜色  
  32.   
  33.      lvi.SubItems.Add("第2列,第"+i+"行");  
  34.   
  35.      lvi.SubItems.Add("第3列,第"+i+"行");  
  36.   
  37.      man_lvg.Items.Add(lvi);   //分组添加子项  
  38.   
  39.      // 或 lvi.Group = man_lvg;  //分组添加子项  
  40.   
  41.      this.listView1.Items.Add(lvi);  
  42. }  

运行效果:

          2、查找文本(只能查找到匹配前缀的文本且只能找出第一个符合的项):

[csharp]  view plain copy print ?
 
  1. ListViewItem foundItem= this.listView1.FindItemWithText(this.textBox1.Text,true,0);    //参数1:要查找的文本;参数2:是否子项也要查找;参数3:开始查找位置 
  2.  
  3. if (foundItem != null) 
  4.  
  5.    this.listView1.TopItem = foundItem;  //定位到该项 
  6.  
  7.    foundItem.ForeColor = Color.Red;   
 
16
 
0
 
 
 
我的同类文章
 
猜你在找
查看评论
7楼  chenpeng32016-07-13 09:32发表 [回复]
赞一个
6楼  qq_344643932016-03-29 09:52发表 [回复]
你好,我设置完行高后,图标为什么就没有了呢?还有如何在第二列加图标呢?
5楼  唐基柯徳2015-12-11 09:46发表 [回复]
很不错,赞一个
4楼  LoveAlone2015-10-15 18:04发表 [回复]
谢谢楼主分享,很有用
3楼  牧原2015-06-29 14:51发表 [回复]
在ListView的Details视图中,第一列只能显示一个图标或CheckBox,不能两者都显示吗???若可以的话求详解~~~~谢谢!!!!!
2楼  z5440174392014-05-26 15:56发表 [回复]
无解
1楼  沉思的雅典娜2013-02-26 15:35发表 [回复]
学习啦
 
* 以上用户言论只代表其个人观点,不代表优快云网站的观点或立场
 
 
 
 
  • 个人资料
 
 
    • 访问:304175次
    • 积分:2978
    • 等级: 
    • 排名:第8099名
    • 原创:162篇
    • 转载:126篇
    • 译文:0篇
    • 评论:33条
  • 文章分类
  • 最新评论
 
 
 

转载于:https://www.cnblogs.com/onlyforliu/p/5722374.html

非常好用的CListCtrl加强版。 从CListCtrl继承,完全兼容CListCtrl. 它有以下特性: • The background of the sorted column can be displayed in a different color (like the detailed view of Windows XP Explorer). • The header of the sorted column can display an arrow that indicates the sort direction (like the detailed view of Windows XP Explorer). • You can give the user the opportunity to hide or redisplay selected columns by just clicking on the header control with the right mouse button (see picture below). • You can hide or redisplay a selected column entirely. • In contrast to the original list view control, the first column can have the LVCFMT_CENTER or LVCFMT_RIGHT style, too. For this feature, the list view control must have the LVS_OWNERDRAWFIXED style. • The label attributes state icon, small icon, and selection can always be shown in the leftmost column, independent of the order of the columns. For this feature, the listview control must have the LVS_OWNERDRAWFIXED style. • You can supply tooltips not only for the whole item, but also for the small icon, the state icon, and each subitem label. • The extended styles LVS_EX_CHECKBOXES, LVS_EX_ONECLICKACTIVATE, LVS_EX_SUBITEMIMAGES, LVS_EX_TWOCLICKACTIVATE, and LVS_EX_UNDERLINEHOT will be supported even if the list view control has the LVS_OWNERDRAWFIXED style. • If the LVS_EX_CHECKBOXES style has been applied and a selected item will be checked/unchecked, all other selected items will be checked/unchecked, too. • If the LVS_EX_LABELTIP style has been applied, not only the partially hidden text of the item label will be unfolded but also the partially hidden text of each subitem label. Note: The LVS_EX_LABELTIP style will be supported under all operating systems. • Tooltips and expanded labels can be displayed simultaneously. • The current state of the list view control (column widths, column order, hidden columns, and sort column and direction) can be saved and restored.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值