c#中带有图像的列表视图

本文关键字:列表 视图 图像 | 更新日期: 2023-09-27 18:03:44

我希望制作一个带有图片的项目列表,项目的数量可以从1-60不等,对于每个项目,我希望也显示数据。我认为最好的方法是使用c#中的ListView。这是真的吗?如果是真的,我该怎么做呢?我还考虑过在滚动窗口内使用交互式图像

c#中带有图像的列表视图

如果要在设计器中执行此操作,可以执行以下步骤来添加

    切换到设计器,点击组件托盘上的ImageList组件,在ImageList的右上角会出现一个智能标签。
  1. 点击智能标签,点击面板上的"选择图片"。在弹出的图像收集编辑器对话框中,从文件夹中选择图像你想要的东西。
  2. 点击OK完成ImageList的添加。点击表单上的ListView,会有一个智能标签出现在右上角角落。
  3. 点击智能标签,你会发现那里有三个组合框,选择一个
  4. 点击智能标签上的"添加项目"选项,一个ListViewItem Collection Editor会出现,你可以向ListView添加项目,这里设置ImageIndex或ImageKey属性,否则图像将不显示
  5. 单击OK完成项目编辑,现在您会发现图像显示在ListView。

如果你想通过代码将图像添加到ListView,你可以这样做'

<<p> 代码片段/strong>
 private void Form10_Load(object sender, EventArgs e)
    {
        DirectoryInfo dir = new DirectoryInfo(@"c:'pic");
        foreach (FileInfo file in dir.GetFiles())
        {
            try
            {
                this.imageList1.Images.Add(Image.FromFile(file.FullName));
            }
            catch{
                Console.WriteLine("This is not an image file");
            }
        }
        this.listView1.View = View.LargeIcon;
        this.imageList1.ImageSize = new Size(32, 32);
        this.listView1.LargeImageList = this.imageList1;
        //or
        //this.listView1.View = View.SmallIcon;
        //this.listView1.SmallImageList = this.imageList1;
        for (int j = 0; j < this.imageList1.Images.Count; j++)
        {
            ListViewItem item = new ListViewItem();
            item.ImageIndex = j;
            this.listView1.Items.Add(item);
        }
    }