Solved

ImageList into ListView vb.net

Posted on 2006-11-02
12
6,017 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

821 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