0

几个小时以来,我一直试图找出如何使用菜单来选择图片,例如列表中是否有胡萝卜;当你点击它时,它旁边会有一张胡萝卜的图片。我对visual basic(我刚开始)不是很有经验,所以我需要你们的帮助。我已经有一些代码,但我认为它不起作用

 Dim imgList As New ImageList
  ListView1.View = View.Details
  ListView1.Width = 500
  ListView1.Columns.Add("Name", 100, HorizontalAlignment.Left)
  ListView1.AllowColumnReorder = True
  ListView1.Columns(0).DisplayIndex = 1
  ImageList1.Images.Add("Name", Image.FromFile("C:\pic\test.jpeg"))
  ListView1.SmallImageList = ImageList1
4

1 回答 1

0

You should set the ListViewItem.ImageIndex to the Index of the image in the ImageList, when adding a new ListViewItem it`s as simple as:

ImageList1.Images.Add("Key", Image.FromFile("C:\pic\test.jpeg"))
Dim newItem = New ListViewItem("Item Name", ImageList1.Images.IndexOfKey("Key"))
ListView1.Items.Add(newItm)

When dealing with already added items you can set it directly, by refrence, index or what have you:

'set ListViewItem.ImageIndex for ListViewItem at index 0
'using ListViewItem.Tag can be usefull for this
ListView1.Items(0).Tag = New String("Key")
ListView1.Items(0).ImageIndex = ImageList1.Images.IndexOfKey(ListView1.Items(0).Tag.ToString)
于 2013-08-25T11:29:27.400 回答