如何匹配列表视图中的项目和图像列表中的图像 [英] How to match items in listview and images in imagelist

查看:122
本文介绍了如何匹配列表视图中的项目和图像列表中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次发帖,而且我不是英语母语人士,所以请不要为我的英语不好而发火.

我正在使用Visual Studio 11,并制作一个Windows窗体应用程序(如pdf文件管理软件).现在,我可以将pdf文件添加到listveiw,并将pdf文件的首页保存为缩略图.

但是我不知道要匹配imagelist中的图像和listview中的项目.

在列表视图中添加项目时,我匹配了项目数和imagelist的索引,并且似乎可以执行此功能,但是如果我删除项目,则应该删除相应的图像,但是我不知道该怎么做. (这是最好的方法吗?)

This is my first time to post and I''m non-native speaker of English, so no flames about my bad English please.

I''m using visual studio 11 and making a windows form application like a pdf file management software. Now, I can add pdf files to listveiw and save the top page of pdf files in imagelist as a thumbnail.

But I have no idea to match images in imagelist and items in listview.

When adding items in listview, I matched the number of items and index of imagelist and I seemed to make the function, but if I remove items, the corresponding image should be removed but I don''t know how to do this. (Is this best way?)

int VolNum = listView1.Items.Count; //counting items in listview1
ListViewItem itm = listView1.Items.Add(fileName, VolNum); //match items(fileName) and images in imagelist



此外,我将列表视图中的项目导出到CSV,并在启动时加载CSV文件.请告诉我如何匹配列表视图中的项目和图像列表中的图像.
我使用此代码读取CSV.



Moreover, I export items in listview to CSV, and load the CSV file on start-up. Please tell me how to match items in listview and images in imagelist.
I use this code to read CSV.

StreamReader srInf = new StreamReader(@"C:\data.csv");

  String filedata;

  while ((filedata = srInf.ReadLine()) != null)
  {

      string[] csvdata = filedata.Split(',');


      ListViewItem Listdata = new ListViewItem();
      Listdata.Text = csvdata[0];
      Listdata.SubItems.Add(csvdata[1]);
      Listdata.SubItems.Add(csvdata[2]);
      listView1.Items.Add(Listdata);
  }

  srInf.Close();

推荐答案

看看 ObjectListView [^ ]
这是文章更易于使用ListView [自定义绘画 [
Have a look at ObjectListView[^]
Here is an article A Much Easier to Use ListView[^].

If you really want to use the standard listview, have a look at custom painting[^].

Best regards
Espen Harlinn


您的英语还不错;我希望其他成员可以用一种清晰的语言发布他们的问题,而不会出现难看的拼写错误.但是,您的问题看起来是人为的.您首先将一些不匹配的数据放入控件中,之后又想匹配项目.为什么?

您应该执行以下操作:您需要将数据层与UI分开.当您想在数据中找到某种关系的实际数据元素时,可以在数据中找到它,而不是在UI控件中.

对于您的问题,您可以执行以下操作之一:1)不要将图像和文件名分开存储,使用包含两者的结构列表进行处理; 2)创建一个字典,例如由文件名索引的System.Collection.Generic.Dictionary<string, Image>(例如,在用作键之前将它们大写);即可通过文件名快速进行O(1)数据搜索(如果您不知道"Big O"符号,请阅读
^ ]).

获得正确的数据模型后,请考虑将数据绑定到UI.在最简单但最灵活的情况下,它可能只有两种方法:从数据填充UI并从UI更新数据,但是有可能,您可能需要更复杂的UI行为,但是仍然可以使用数据模型来完成,不只是UI或UI中的数据.基于这些理由,还必须认真地重新考虑您的UI设计.我不知道您的细节,但是如果您改变方法,进行一些开发但遇到问题,请提出进一步的问题.我很有可能会提供帮助,其他专家也可能会提供帮助.

—SA
You English is fine; I wish other members could post their question in such a clear language free from ugly spelling mistakes. However, your problem looks artificial. You put some unmatched data in controls in first place and later wants to match items. Why?

You should do the following: you need to separate your data layer from UI. When you want to find anything in data, an actual data element of some relationship, find it in data, not in the UI control.

As to your problems, you can do one of the following: 1) do not store images and file names separate, work with a list of structures containing both; 2) create a dictionary, such as System.Collection.Generic.Dictionary<string, Image> indexed by the file names (say, lo-case them before using as a key); that will give you a fast O(1) search of data by the file name (if you don''t know "Big O" notation, read http://en.wikipedia.org/wiki/Big_O_notation[^]).

After you got a proper data model, think about data binding to your UI. In a simplest but most flexible case, it could be as simple as two method: population of UI from data and update data from UI, but chances are, you might need more complex UI behavior, but nevertheless, do it using the data model, not just UI or data in UI. On these grounds, also critically re-think you UI design. I don''t know your detail, but if you change your approach, do some development but face with problems, ask your further questions. Most likely I would be able to help, other experts will probably help, too.

—SA


这篇关于如何匹配列表视图中的项目和图像列表中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆