WindowForm component ListView

ListView
1. Attributes
① CheckBox with check boxes in each row, ② View 5 kinds of views, ③ Alignment content item alignment
④ ShowGroups display items in the form of groups ⑤ Groups group collection (similar to my computer hard disk group under c, d, e Drive letter) ⑥Items collection ⑦MultiSelect allows multiple selections
⑧SmallImageList ImageList control for all view images except the large icon view and Tile view
⑨Detailed view specific properties (View -> Detials): AllowColumnRecorder allows column sorting, Columns column name, GridLines grid
⑩ large icon view LargeIcon: Auto arrange icons picture AutoArrange LargeImageList large collection of
small icons view SmallIcon: AutoArrange
CheckedIndices checked index collection, the collection CheckedItems hook option, selectedIndices selected index set, SelectedItems selected collection of items
* List view Group display is not supported
2. Event
SelectedIndexChanged When the selection changes, the ColumnClick column header changes when the ItemChecked state changes
ListView control reference article
Large icon view

Dictionary<string, string> dicGroup = new Dictionary<string, string>();
//初始化
private void FrmListView2_Load(object sender, EventArgs e)
{
    
    
    LoadImgList();		//加载图片
    //初始化分组信息
     lvList.Groups.Clear();
    lvList.Groups.Add(new ListViewGroup("花", HorizontalAlignment.Center));
    lvList.Groups.Add(new ListViewGroup("动物", HorizontalAlignment.Center));
    lvList.Groups.Add(new ListViewGroup("人物", HorizontalAlignment.Center));
    lvList.Groups.Add(new ListViewGroup("风景", HorizontalAlignment.Center));
    lvList.Items.Clear();
    //关系存储
    dicGroup.Add("花", "04");
    dicGroup.Add("动物", "01");
    dicGroup.Add("人物", "02");
    dicGroup.Add("风景", "03");

    lvList.ShowGroups = false;			//是否分组显示
    if (largeList!=null && largeList.Images.Count >0)
    {
    
    
        for(int i=0;i<largeList.Images.Count;i++)
        {
    
    
            string iText = largeList.Images.Keys[i];
            //添加ListView控件中的项
            ListViewItem li = new ListViewItem();
            li.Text = iText;
            li.ImageIndex =i;
            lvList.Items.Add(li);
        }
    }
    lvList.View = View.LargeIcon;		//指定视图模式
    lvList.LargeImageList = largeList;	//大图标使用的ImageList
    lvList.SmallImageList = smallList;	//除大图标以外所有视图使用的ImageList
}

Insert picture description here

Detail view

private void btnDetails_Click(object sender, EventArgs e)
{
    
    
    lvList.Items.Clear();
    lvList.Columns.Clear();
    //lvList.ShowGroups = false;
    lvList.View = View.Details;			//设置详细信息视图
    //列的添加
    lvList.Columns.Add("文件名", 100, HorizontalAlignment.Left);
    lvList.Columns.Add("创建日期", 150, HorizontalAlignment.Left);
    lvList.Columns.Add("类型", 80, HorizontalAlignment.Left);
    lvList.Columns.Add("大小", 60, HorizontalAlignment.Left);
    //项
    for (int i = 0; i < dic.Count; i++)
    {
    
    
        ListViewItem li = new ListViewItem();
        li.ImageIndex = i;
        li.Text = smallList.Images.Keys[i];
        li.SubItems.Add(File.GetCreationTime(dic[i]).ToString());//创建日期
        li.SubItems.Add(Path.GetExtension(dic[i]));				//类型
        long length = new FileInfo(dic[i]).Length;				//获取文件大小 B
        li.SubItems.Add((length / 1024).ToString());			//KB  
        if(lvList.ShowGroups)
        {
    
    
            foreach (ListViewGroup lvg in lvList.Groups)
            {
    
    
                //判断项文本的前面的编号是否与组所对应的编号一致,如果一致,就将该项分到这一组
                if (li.Text.Substring(0, 2) == dicGroup[lvg.Header])
                {
    
    
                    li.Group = lvg;						//设置项所属的组
                    break;
                }
            }
        }
        lvList.Items.Add(li);
    }
    lvList.GridLines = true;						//显示网格
}

Insert picture description here

Grouped view

//分组
private void btnGroup_Click(object sender, EventArgs e)
{
    
    
     lvList.ShowGroups = true;      
     GroupShow();
 }
 //分组处理
 private void GroupShow()
 {
    
    
      for (int i = 0; i < lvList.Items.Count; i++)
      {
    
    
          foreach (ListViewGroup lvg in lvList.Groups)
          {
    
    
              //判断项文本的前面的编号是否与组所对应的编号一致,如果一致,就将该项分到这一组
              if (lvList.Items[i].Text.Substring(0, 2) == dicGroup[lvg.Header])
              {
    
    
                  lvList.Items[i].Group = lvg;//设置项所属的组
                  break;
              }
          }
      }
  }

Insert picture description here

Guess you like

Origin blog.csdn.net/asdasd1fdsyrt/article/details/113923178