Solved

ImageList into ListView vb.net

Posted on 2006-11-02
12
6,083 Views
Last Modified: 2008-02-01
Hi Experts.

I've created a WinForm with a ListView which imports data from a text file into columns. So far so good. What I want to do is add a bitmap to the 1st column. The bitmap will be from a file also.  I have setup the imageList to point to a specific bmp.

Here's my code.
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim fileName As String = "c:\test.txt"
        If System.IO.File.Exists(fileName) Then
            ListView1.BeginUpdate()
            ListView1.Items.Clear()

            ListView1.View = View.Details

            Dim inputLine As String
            Dim sr As New System.IO.StreamReader(fileName)
            Dim header As Boolean = True
            Dim value As String
            Dim values() As String
            Dim ch As ColumnHeader

            inputLine = sr.ReadLine
            While Not IsNothing(inputLine)
                ' replace double quotes with single quotes
                inputLine = Replace(inputLine, Chr(34) & Chr(34), Chr(34))
                ' remove leading and trailing quotes
                inputLine = inputLine.Substring(1, inputLine.Length - 2)
                ' create an array from the values
                values = Split(inputLine, Chr(34))

                If header Then
                    header = False
                    For Each value In values
                        ch = New ColumnHeader
                        ch.Text = value
                        ListView1.Columns.Add(ch)
                        ch.Width = 70
                        ImageList1.Images.Add(Bitmap.FromFile("c:\buddy.bmp"))  'Here is the code i added!!***********
                    Next
                Else
                    ' create a new listview item with the values
                    ListView1.Items.Add(New ListViewItem(values))
                End If

                ' read the next line
                inputLine = sr.ReadLine
            End While

            ListView1.EndUpdate()
        Else
            MessageBox.Show(fileName, "File Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

I also added 'ListView1.SmallImageList = ImageList1' but I aint getting no images appear in the first column  :(
0
Comment
Question by:RobertoFreemano
[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
  • 8
  • 3
12 Comments
 
LVL 4

Expert Comment

by:hclgroup
ID: 17861145
0
 

Author Comment

by:RobertoFreemano
ID: 17861507
Hi hclgroup,

Thanks for the Link, but it only show the basics of the Collection Editors. I've already looked into this.

Thanks anyway.

Roberto
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17861871
Right...first you set the ImageList:

    ListView1.SmallImageList = ImageList1

Then you need to set the ImageIndex() property for EVERY single ListViewItem.

This can be done in the constructor:

    ListView1.Items.Add(New ListViewItem(values, 0))

The 0 is the image index to use from the ImageList.

Or you can create the ListViewItem seperately and modify it before adding it to the ListView:

    Dim lvi As New ListViewItem(values)
    lvi.ImageIndex = 0
    ListView1.Items.Add(lvi)
0
Technology Partners: 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!

 

Author Comment

by:RobertoFreemano
ID: 17862385
That works a treat,

Thanks Idle_Mind.

I would like to award you the points, but I'm willing to increase if you could assist with an extension to my little vb project?

I won't be offended if you do not wish to assist! you can still have the 80 points!

Roberto
0
 

Author Comment

by:RobertoFreemano
ID: 17862453

So far, the text format that is loaded into the columns are displayed as

"username""employeeid""Tel""
"Bob""18273456""01925etc"

I now have an image to display each user thanks to Idle_Mind. I would like to change the image in a particular row that displays the word 'Unknown' in one of the subitems... example

 username    employeeid     Tel""
 :)  Bob         18273456        01925etc"
 :)  Bill          18299456        01925etc"
 :(  Gill          2827344          Unknown"

I understand the theory, but not sure how to achieve.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17862814
Will the "Unknown" be in a specific column? ...or do you want the different icon if ANY of the columns has it?

Probably the latter:

    Dim lvi As New ListViewItem(values)
    lvi.ImageIndex = 0 ' Assume it is all good...
    For each value As String in values
        If value.ToLower.IndexOf("unknown") <> -1 Then
            lvi.ImageIndex = 1 ' ...until proven otherwise
            Exit For
        End If
    Next
    ListView1.Items.Add(lvi)
0
 

Author Comment

by:RobertoFreemano
ID: 17863154

Hi,

The Icon to change if 'Unknown' is in any of the other columns.

Roberto
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 17863221
See my previous post...

Though, you could check "inputLine" directly if you want:

    Dim lvi As New ListViewItem(values)
    If inputLine.ToLower.IndexOf("unknown") = -1 Then
        lvi.ImageIndex = 0 ' Index of Smiley
    Else
        lvi.ImageIndex = 1 ' Index of Frown
    End If
    ListView1.Items.Add(lvi)
0
 

Author Comment

by:RobertoFreemano
ID: 17863254
Hi,

when I tryped:

1 Dim lvi As New ListViewItem(values)
2   lvi.ImageIndex = 0 ' Assume it is all good...
3    For each value As String in values
4       If value.ToLower.IndexOf("unknown") <> -1 Then
5           lvi.ImageIndex = 1 ' ...until proven otherwise
6          Exit For
7     End If
8 Next
9  ListView1.Items.Add(lvi)

Name 'Values' is not declared message on lines 1 & 3

Any ideas?
0
 

Author Comment

by:RobertoFreemano
ID: 17863255
is it because I'm on vb.net 2003?
0
 

Author Comment

by:RobertoFreemano
ID: 17863289
It works
0
 

Author Comment

by:RobertoFreemano
ID: 17863295
Cheers Idle_Mind.

You're a Genius!
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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.

751 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