想要避免列表视图中的重复图像 C# .NET
我第一次使用 imagelist 和 listview 组件,我想要的是列出图像。我面临的问题是我在再次将图像添加到列表视图时无法避免重复的图像。请参阅下面的代码并让我知道哪里出错了
OpenFileDialog addImages = new OpenFileDialog();
addImages.Filter = "JPEG (*.jpg)|*.jpg";
addImages.Multiselect = true;
if (addImages.ShowDialog(this) == DialogResult.OK)
{
foreach (string filename in addImages.FileNames)
{
try
{
if (this.imageList1.Images.ContainsKey(filename) == false)
{
this.imageList1.Images.Add(filename, Image.FromFile(filename));
}
}
catch{}
}
this.listView1.View = View.LargeIcon;
this.listView1.LargeImageList = this.imageList1;
for (int i = 0; i < this.imageList1.Images.Count; i++)
{
if (this.listView1.Items.ContainsKey(this.imageList1.Images.Keys[i]) == false)
{
ListViewItem li = new ListViewItem();
li.ImageIndex = i;
li.ImageKey = this.imageList1.Images.Keys[i];
li.Text = Path.GetFileName(this.imageList1.Images.Keys[i]);
this.listView1.Items.Add(li);
}
}
}
I am using imagelist with listview component for the firs time, what i wanted is to list images. the problem i am facing is i couldn't avoid duplicate images while adding images again to listview. please see following code and let me know where i am going wrong
OpenFileDialog addImages = new OpenFileDialog();
addImages.Filter = "JPEG (*.jpg)|*.jpg";
addImages.Multiselect = true;
if (addImages.ShowDialog(this) == DialogResult.OK)
{
foreach (string filename in addImages.FileNames)
{
try
{
if (this.imageList1.Images.ContainsKey(filename) == false)
{
this.imageList1.Images.Add(filename, Image.FromFile(filename));
}
}
catch{}
}
this.listView1.View = View.LargeIcon;
this.listView1.LargeImageList = this.imageList1;
for (int i = 0; i < this.imageList1.Images.Count; i++)
{
if (this.listView1.Items.ContainsKey(this.imageList1.Images.Keys[i]) == false)
{
ListViewItem li = new ListViewItem();
li.ImageIndex = i;
li.ImageKey = this.imageList1.Images.Keys[i];
li.Text = Path.GetFileName(this.imageList1.Images.Keys[i]);
this.listView1.Items.Add(li);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用函数“ContainsKey”进行重复。
MSDN 说:“ Name 属性对应于 ListView.ListViewItemCollection 中 ListViewItem 的键。”
因此,您需要设置 ListViewItem 的名称。
You used the function "ContainsKey" for duplicate.
MSDN say : " The Name property corresponds to the key for a ListViewItem in the ListView.ListViewItemCollection."
So you need to set the name of your ListViewItem.
我不完全理解你的问题,但你可以做一些事情。
在函数中的 for 循环之前添加一个
this.listView1.Items.Clear()
。下次打开 OpenFileDialog 时,您将向空列表视图添加项目。在每个 LiveViewItem 标记中添加文件名。
li.Tag = ...文件路径...
。然后,每次向 listView 添加新项目时,只需检查它是否尚未包含带有该标签的项目。I don't fully understand your question, but there are a few things that you could do.
add a
this.listView1.Items.Clear()
in the function, before the for-loop. Next time you open the OpenFileDialog you will add items to an empty listview.add the filename in each LiveViewItem tag.
li.Tag = ...filepath...
. Then every time you add a new item to the listView, just check if it doesn't already contain an item with that tag.