Solved

ImageList into ListView vb.net

Posted on 2006-11-02
12
6,035 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
  • 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 85

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

 

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 85

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 85

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 100
topping3 challenge 14 90
how to update exe applicatio from internet ? 6 86
Getting the Error "User-defined type not defined" in MS Access 2013 16 72
This article will show, step by step, how to integrate R code into a R Sweave document
A short article about a problem I had getting the GPS LocationListener working.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

839 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