Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1239
  • Last Modified:

Using ImageList to supply icons to a ListView, how to initialize?

I have an ImageList and a ListView control on my form, and I simply want to use the icons from the ImageList for the ListView.  However, when I do:

dim lvitem as listitem
lvitem.Icon = ImageList1.ListImages(2)

I get an error saying I must Initialize the ImageList before using it.  How do I initialize it?  BTW I am adding icons and design-time.

Thanks, Matt
0
bomax
Asked:
bomax
  • 6
  • 5
1 Solution
 
JohnMcCannCommented:
At design time you set the image list for the listview by clicking the right mouse on the list view and selecting properties.  This will bring up the properties window.  Select the tab labeled image list, you can use the two drop down list boxes to set the image lists for the listview control.

To use the listview

ListView1.ListItems.Add , , "Something", 1, 1

0
 
bomaxAuthor Commented:
I have already done this but still it says I need to "initialize" the imagelist before I can use it.
0
 
spauljosephCommented:
Use this.

Set ListView1.ImageList = ImageList1

or this.

Set lvitem.ImageList = ImageList1
'According to your code.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bomaxAuthor Commented:
Hmm...okay now it is highlighting that line and saying "Invalid Key" ?
0
 
JohnMcCannCommented:
Have you added any images to the imagelist as that is how it is initialized.

You do not use this to work with the LiatView

lvitem.Icon = ImageList1.ListImages(2)

It is used as follows

ListView1.ListItems.Add , , "Something", 1, 1
or
lvitem.Icon = 1
or
lvitem.SmallIcon = 2

Make sure you did as I stated above.

If you are still having problems try the following and see if this helps to identify where you are going qrong.

A) Start VB
B) Create new exe
3) Right click on the toolbox select components.
4) Scrool down to Micorosft Common Controls 6
5) Put a tick in the box and press ok
6) Double click on the ListView (Creates ListView 1)
7) Double click on image list creates ImageList1.
8) Fill the imagelist with 5 images.
8) Click the right mouse button on the ListView.
9) Select Properties and assign the image list to the large and small icons.

10) In the Form_Load event enter the folloeing

ListView1.ListItems.Add , , "First", 1, 1
ListView1.ListItems.Add , , "Second", 2, 2
ListView1.ListItems.Add , , "Third", 3, 3




0
 
bomaxAuthor Commented:
works like a charm...thank you!
0
 
bomaxAuthor Commented:
One more quick question, how do I change the icon at a later time?
0
 
JohnMcCannCommented:
lvitem.Icon = Key
or
lvitem.SmallIcon = Key

Where key is either a numeric value indicating the icons index or a string denoting the key defined in the imagelist.

0
 
bomaxAuthor Commented:
Well lets say I want to change the icon for the 3rd item in my listview.  What is the complete code for that?
0
 
JohnMcCannCommented:
ListView1.ListItems(1).Icon = 3
ListView1.ListItems(1).SmallIcon = 3

Icon is large icons SmallIcon is small icons.
0
 
bomaxAuthor Commented:
Thanks!
0
 
JohnMcCannCommented:
Ok
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now