带有图像的 C# 中的列表视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15366294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
listview in c# with images
提问by Froodle
I wish to make a list of items with pictures, the amount of items can vary from 1-60 and for each item I wish to also show data. I believe the best way of going about this is using the ListView in c#. is this true and if so how would I go about doing this? i have also thought about using interactive images within a scrolling window
我想用图片制作一个项目列表,项目的数量可以从 1-60 不等,对于每个项目,我还希望显示数据。我相信最好的方法是在 c# 中使用 ListView。这是真的吗,如果是这样,我将如何去做?我还考虑过在滚动窗口中使用交互式图像
采纳答案by Mido
If you want to do this in the designer, you can take the following steps to add the images to the ListView control:
如果要在设计器中执行此操作,可以执行以下步骤将图像添加到 ListView 控件:
- Switch to the designer, click on the ImageList component on the Component Tray, there will be a smart tag appear on the top-right corner of the ImageList.
- Click the smart tag, and click "Choose Images" on the pane.
- On the pop-up Image Collection Editor dialog, choose the images from the folder your want.
- Click OK to finish adding images to the ImageList.
- Click the ListView on the form, there will be a smart tag appear on the top-right corner.
- Click the smart tag, you will find there're three ComboBoxes there, choose a ImageList from the list as you want.
- Click the "Add items" option on the smart tag, a ListViewItem Collection Editor will appear, you can add items to the ListView, it's important here to set the ImageIndex or ImageKey property, or the image won't appear.
- Click OK to finish item editing, now you'll find the images are displayed on the ListView.
- 切换到设计器,点击Component Tray上的ImageList组件,ImageList的右上角会出现一个智能标签。
- 单击智能标记,然后单击窗格上的“选择图像”。
- 在弹出的图像集编辑器对话框中,从您想要的文件夹中选择图像。
- 单击确定完成将图像添加到 ImageList。
- 点击窗体上的ListView,右上角会出现一个智能标签。
- 单击智能标签,您会发现那里有三个 ComboBox,您可以从列表中选择一个 ImageList。
- 点击智能标签上的“添加项目”选项,会出现一个ListViewItem集合编辑器,可以向ListView添加项目,这里设置ImageIndex或者ImageKey属性很重要,否则图片不会出现。
- 单击“确定”完成项目编辑,现在您会发现图像显示在 ListView 上。
If you want to add the images to the ListView by code, you can do something like this`
如果你想通过代码将图像添加到 ListView,你可以这样做`
Code Snippet
代码片段
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);
}
}