PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] ListView - Row Picture-VBForums
Results 1 to 11 of 11

Thread: [RESOLVED] ListView - Row Picture

  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Resolved [RESOLVED] ListView - Row Picture

    Hello i was wondering i need some help how can add a picture to on left side of listview on each row that is added i have a imagelist and the listviews largeimagelist is set to my imagelist with the pic on it do you even need a image list for this aswell thanks


    Code:
         Public Function ProcessJunkFiles(ByVal L As ListView, ByVal Path As String)
            Dim list As List(Of String) = GetFilesRecursive(Path)
            Try
                ' Loop through and display each path.
                For Each Item As String In list
                    If Not IsSpecialFolder(Item) And Not IsFileInUse(Item) Then
                        Dim arr As String() = New String(4) {}
                        Dim itm As ListViewItem
                        'add items to ListView
                        Application.DoEvents()
                        arr(0) = AbsoluteFilePath(Item)
                        arr(1) = FormatBytes(Item)
                        arr(2) = FilePathExt(Item)
                        arr(3) = Item
                        itm = New ListViewItem(arr)
                        L.Items.Add(itm)
                        L.Items(L.CheckedItems.Count).Checked = True
             
                      'CODE TO ADD PICTURE 
                    End If
                Next
    
            Catch ex As Exception
            End Try
        End Function

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,637

    Re: ListView - Row Picture

    The documentation for the LargeImageList property has this to say:
    The LargeImageList property allows you to specify an ImageList object that contains icons to use when displaying items with large icons (when the View property is set to LargeIcon).
    Have you set the View to LargeIcon? If you're talking about the "left side" of the ListView then I'm guessing not. I'm guessing that you've set it to Details. The documentation for the SmallImageList property has this to say:
    The SmallImageList property allows you to specify an ImageList object that contains icons to use when displaying items with small icons (when the View property is set to any value other than LargeIcon).
    Given that the documentation has that very relevant information, it like has even more, like how to get the appropriate image displayed in each item.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: ListView - Row Picture

    i have the listview set to Details

    this is my imagelist icon im trying to add to the listview
    Name:  sheild.jpg
Views: 113
Size:  21.6 KB


    this is what im talking about the left side how the icons show on this apps listview

    Name:  items.jpg
Views: 87
Size:  39.1 KB
    Last edited by sinner0636; Dec 23rd, 2017 at 02:54 PM.

  4. #4

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: ListView - Row Picture

    ok i tried thon the form load event still nothing comes up with only text no image hmm



    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim imageList As New ImageList()
            imageList.Images.Add(Bitmap.FromFile("C:\My Program\AppIcons\Shield.bmp"))
            Me.ListView1.View = View = View.Details
            Me.ListView1.LargeImageList = imageList
    
            Me.ListView1.Items.Add("test")
    End Sub
    Last edited by sinner0636; Dec 23rd, 2017 at 02:54 PM.

  5. #5
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    465

    Re: ListView - Row Picture

    Quote Originally Posted by sinner0636 View Post
    ok i tried thon the form load event still nothing comes up with only text no image hmm



    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim imageList As New ImageList()
            imageList.Images.Add(Bitmap.FromFile("C:\My Program\AppIcons\Shield.bmp"))
            Me.ListView1.View = View = View.Details
            Me.ListView1.LargeImageList = imageList
    
            Me.ListView1.Items.Add("test", 0)
    End Sub
    I believe you also have to specify the index of the image to use from the ImageList, since an ImageList can contain multiple images. So it needs to know which image to use, even if the ImageList contains only 1 image, like it does in your case. Try making the bolded change above.


    Edit:

    Code:
    Me.ListView1.View = View = View.Details
    I'm assuming the above line was meant to be:

    Code:
    Me.ListView1.View = View.Details
    or
    Code:
    Me.ListView1.View = View.LargeIcon
    Last edited by OptionBase1; Dec 23rd, 2017 at 03:19 PM.

  6. #6
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,532

    Re: ListView - Row Picture

    To setup ImageList properly specify ColorDepth, ImageSize and image index when adding an item to the ListView

    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.         Dim imageList As New ImageList()
    3.         imageList.ColorDepth = ColorDepth.Depth32Bit
    4.         imageList.ImageSize = New Size(32, 32)
    5.         imageList.Images.Add(Bitmap.FromFile("C:\My Program\AppIcons\Shield.bmp"))
    6.         Me.ListView1.View = View.LargeIcon
    7.         Me.ListView1.LargeImageList = imageList
    8.  
    9.         Me.ListView1.Items.Add("test", 0)' show first icon with this item.
    10.     End Sub



  7. #7

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: ListView - Row Picture

    ahh that worked yay

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListView1.CheckBoxes = True
            ListView1.FullRowSelect = True
            Dim imageList As New ImageList()
            'imageList.ColorDepth = ColorDepth.Depth32Bit
            'imageList.ImageSize = New Size(32, 32)
            imageList.Images.Add(Bitmap.FromFile("C:\Micro Pc\AppIcons\Shield.bmp"))
            ListView1.View = View.Details
            ListView1.SmallImageList = imageList
            ListView1.Items.Add("1", ListView1.CheckedItems.Count) ' show first icon with this item.
            ListView1.Items.Add("2", ListView1.CheckedItems.Count) ' show second icon with this item.
    End Sub

    Name:  icons.jpg
Views: 71
Size:  17.4 KB

  8. #8

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: ListView - Row Picture

    But the only problem im having now is loading the picture from a array of file names


    Code:
     Public Function ProcessSpecialFolders(ByVal L As ListView)
            Dim Cookies As String = (Environment.GetFolderPath(Environment.SpecialFolder.Cookies))
            Dim History As String = (Environment.GetFolderPath(Environment.SpecialFolder.History))
            Dim IECache As String = (Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))
            Dim WinTemp As String = (Environment.GetFolderPath(Environment.SpecialFolder.Windows)) + "\Temp"
            Dim Prefetch As String = (Environment.GetFolderPath(Environment.SpecialFolder.Windows)) + "\Prefetch"
            Dim UserTemp As String = (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)) + "\Temp"
    
            Dim Recent As String = (Environment.GetFolderPath(Environment.SpecialFolder.Recent))
            Dim SpecialFolders As String() = {Cookies, History, IECache, WinTemp, Prefetch, UserTemp, Recent}
            Try
                'Path is our starting point
                For Each Path As String In SpecialFolders
                    If DirExists(Path) Then
                        Dim list As List(Of String) = ScanSpecialFolders(Path)
    
                        For Each Item As String In list
                            If Not IsFileInUse(Item) Then
                                Dim arr As String() = New String(4) {}
                                Dim itm As ListViewItem
    
                                'add items to ListView
                                Dim imageList As New ImageList
                                'imageList.ColorDepth = ColorDepth.Depth32Bit
                                'imageList.ImageSize = New Size(32, 32)
                                imageList.Images.Add(Bitmap.FromFile("C:\Micro Pc\AppIcons\Shield.bmp"))
                                L.SmallImageList = imageList
    
                                arr(0) = AbsoluteFilePath(Item)
                                arr(1) = FormatBytes(Item)
                                arr(2) = ProcessType(Item)
                                arr(3) = Item
                                itm = New ListViewItem(arr)
    
                                L.Items.Add(itm) '/////// CANT ADD  L.Items.Add(itm , L.CheckedItems.Count )
    
                                L.Items(L.CheckedItems.Count).Checked = True
    
                            End If
                        Next
                    End If
                Next
            Catch ex As Exception
            End Try
        End Function
    Last edited by sinner0636; Dec 23rd, 2017 at 04:55 PM.

  9. #9
    Hyperactive Member
    Join Date
    Nov 2017
    Posts
    465

    Re: ListView - Row Picture

    Quote Originally Posted by sinner0636 View Post
    But the only problem im having now is loading the picture from a array of file names

    Code:
      itm = New ListViewItem(arr, 0)
      L.Items.Add(itm)
    Try the above code instead.

    I have no idea why you make reference to using L.CheckedItems.Count in your posted code as the second parameter to L.Items.Add

    I trust you understand what L.CheckedItems.Count represents, and you see that you are checking each item in the ListBox as you add them, so L.CheckedItems.Count is increasing after each item is added to the ListBox

    As I mentioned in my post above, the second value passed represents the numeric index (zero based) of the Image in your ImageList. How many images are in your ImageList? 1, with an index of 0.

    So as written your code would add the first item to the ListBox linking it to the image in your ImageList with an index of "Number of items in the ListBox that are currently checked", which is zero at the moment, so ImageList(0) - which would work.

    The next item added to your ListBox is linked to the image in your ImageList with an index of "Number of items in the ListBox that are currently checked", which is now 1, so ImageList(1) - which doesn't work since your ImageList doesn't have a second image in it, and so on.

    If you are going to add other images eventually for different icons for different types/locations of files, add either an Enum or Constants that give meaningful names to the index values and use those names. If you're just going to use one image, hard code the 0 like I showed above.

  10. #10
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,532

    Re: ListView - Row Picture

    The following lines must be before the loop not inside
    vb.net Code:
    1. Dim imageList As New ImageList
    2. 'imageList.ColorDepth = ColorDepth.Depth32Bit
    3. 'imageList.ImageSize = New Size(32, 32)
    4. imageList.Images.Add(Bitmap.FromFile("C:\Micro Pc\AppIcons\Shield.bmp"))
    5. L.SmallImageList = imageList

    Why you refer to the image index by L.CheckedItems.Count? The imageList contains only one image so if L.CheckedItems.Count > 0 then an error will popup



  11. #11

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: ListView - Row Picture

    you guys are awesome thanks you guys are right was trying to auto increase the number like i did on the load event code above because cant add

    item.add("test",1) 2nd row comes out blank
    item.add("test",Listview1.CheckedItems.Count) shows the icon for some reason

    this L.Items(L.CheckedItems.Count).Checked = True auto checks all my checkboxes in my code

    but you guys are right this works
    Code:
    itm = New ListViewItem(arr, 0)
    Name:  SCANNER.jpg
Views: 37
Size:  35.6 KB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width