c# - c# 中带有图像的 ListView

标签 c# listview

<分区>

我想制作一个带有图片的项目列表,项目的数量可以从 1 到 60 不等,并且我希望每个项目都显示数据。 我相信解决此问题的最佳方法是在 C# 中使用 ListView。 这是真的吗?如果是的话,我该怎么做呢? 我也考虑过在滚动窗口中使用交互式图像

最佳答案

如果你想在设计器中这样做,你可以按照以下步骤添加 图片到 ListView 控件:

  1. 切换到设计器,单击组件托盘上的 ImageList 组件, ImageList 的右上角会出现一个智能标签。
  2. 单击智能标记,然后单击 Pane 中的“选择图像”。
  3. 在弹出的图像集编辑器对话框中,从文件夹中选择图像 你想要的。
  4. 单击“确定”完成向 ImageList 添加图像。
  5. 点击窗体上的ListView,右上角会出现一个智能标签 角落。
  6. 点击智能标签,你会发现那里有三个组合框,选择一个 根据需要从列表中选择 ImageList。
  7. 点击智能标签上的“添加项目”选项,一个 ListViewItem 集合编辑器 将出现,您可以将项目添加到 ListView,重要的是在这里设置 ImageIndex 或 ImageKey 属性,否则图片不会出现。
  8. 点击确定完成项目编辑,现在您会发现图像显示在 ListView 。

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

代码片段


 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);
        }
    }

Source

关于c# - c# 中带有图像的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15366294/

相关文章:

c# - ASP.NET MVC 呈现模型不是我期望的方式

c# - “逆向工程”模型优先 - 删除表和列前缀

c# - 将项目添加到通用列表时出现问题

java - 如何在onItemClick 中获取ListView 的值并填写ViewPager?

android - ListView : Android 中第一行的默认选择

java - 即使初始化后列表仍为空

c# - Autofixture - 新列表中基于先前构建的属性的属性

c# - 如何删除使用 UserManager.CreateAsync 创建的用户

android - 为什么我们不能在adapter.insert()方法中传递list对象?

C# 跳过复选框 ListView