Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ImageList into ListView vb.net

Posted on 2006-11-02
12
Medium Priority
?
6,297 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
 

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

963 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