我希望列出带有图片的项目,项目的数量可以从 1 到 60 不等,并且对于每个项目我还希望显示数据。我相信解决这个问题的最好方法是在 c# 中使用 ListView。这是真的吗?如果是这样,我该怎么做?我还考虑过在滚动窗口中使用交互式图像
问问题
34590 次
1 回答
10
如果要在设计器中执行此操作,可以采取以下步骤将图像添加到 ListView 控件:
- 切换到设计器,点击Component Tray上的ImageList组件,ImageList右上角会出现一个智能标签。
- 单击智能标签,然后单击窗格上的“选择图像”。
- 在弹出的图像集编辑器对话框中,从您想要的文件夹中选择图像。
- 单击确定以完成将图像添加到 ImageList。
- 点击表单上的ListView,右上角会出现一个智能标签。
- 点击智能标签,你会发现那里有三个ComboBox,你可以从列表中选择一个ImageList。
- 点击智能标签上的“添加项目”选项,会出现一个ListViewItem Collection Editor,您可以向ListView添加项目,这里设置ImageIndex或ImageKey属性很重要,否则图像不会出现。
- 单击确定完成项目编辑,现在您会发现图像显示在 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);
}
}
于 2013-03-12T16:16:57.640 回答