Link to home
Start Free TrialLog in
Avatar of RobertoFreemano
RobertoFreemanoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ImageList into ListView vb.net

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  :(
Avatar of hclgroup
hclgroup

Avatar of RobertoFreemano

ASKER

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
Avatar of Mike Tomlinson
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)
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

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.
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)

Hi,

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

Roberto
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
is it because I'm on vb.net 2003?
It works
Cheers Idle_Mind.

You're a Genius!