?
Solved

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

Posted on 2003-03-08
12
Medium Priority
?
1,195 Views
Last Modified: 2007-12-19
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
Comment
Question by:bomax
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8094980
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
 

Author Comment

by:bomax
ID: 8096407
I have already done this but still it says I need to "initialize" the imagelist before I can use it.
0
 
LVL 2

Expert Comment

by:spauljoseph
ID: 8096583
Use this.

Set ListView1.ImageList = ImageList1

or this.

Set lvitem.ImageList = ImageList1
'According to your code.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:bomax
ID: 8097312
Hmm...okay now it is highlighting that line and saying "Invalid Key" ?
0
 
LVL 5

Accepted Solution

by:
JohnMcCann earned 200 total points
ID: 8097673
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
 

Author Comment

by:bomax
ID: 8098752
works like a charm...thank you!
0
 

Author Comment

by:bomax
ID: 8098778
One more quick question, how do I change the icon at a later time?
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8099441
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
 

Author Comment

by:bomax
ID: 8101078
Well lets say I want to change the icon for the 3rd item in my listview.  What is the complete code for that?
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8102939
ListView1.ListItems(1).Icon = 3
ListView1.ListItems(1).SmallIcon = 3

Icon is large icons SmallIcon is small icons.
0
 

Author Comment

by:bomax
ID: 8112281
Thanks!
0
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8112399
Ok
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question