Avatar of pmathis
pmathis
 asked on

Listview with large icons

I haven't worked that much with listview and was looking for an example that would contain some specifics.  I would like to be able to make a selection in one listbox and have a display occur in the listview from a table.  I have this much working, but was trying to change the listview to display large graphical thumbnails that are pathed to a specific directory from the records that now appear.  The selections from the record set would contain the specific filenames  and path to appear in the listview.  (right now I just have text selections appearing in the listview). Hope this makes sense.   a specific example containing a listview would be great.  I was thinking this would work best in vb6, rather than vb.net.
Visual Basic Classic

Avatar of undefined
Last Comment
Ryan Chong

8/22/2022 - Mon
Ryan Chong

pmathis

ASKER
this is sort of what I was trying to do, is something like this possible, I can't quite get it to work.

    ' Add items and subitems to list control.
'Set ListView1.Icons = ImageList5
    rs.MoveFirst
    Do Until rs.EOF
    'load image path
    ImageList5.ListImages.Add , , LoadPicture(rs!Text)
       ' then set icon for each record
       '  this doesn't work --Set lstItem = Me.ListView1.ListItems.Add(, , rs!employeeid, 1, 1)
        Set lstItem = Me.ListView1.ListItems.Add()
        lstItem.Text = rs!employeeid
        lstItem.SubItems(1) = Nz(rs!TitleOfCourtesy, "N/A")
        lstItem.SubItems(2) = Nz(Trim(rs!LastName))
        lstItem.SubItems(3) = Nz(Trim(rs!FirstName))
        lstItem.SubItems(4) = Format(rs!HireDate, "Medium Date")

        rs.MoveNext
    Loop
    'close recordset
    rs.Close
    DoCmd.Echo True
ErrorHandlerExit:
    Exit Sub
ErrorHandler:
    If Err = 3021 Then    ' no current record
        Resume Next
    Else
        MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
        Resume ErrorHandlerExit
    End If

End Sub
Private Sub cmdExit_Click()
    On Error GoTo Err_cmdExit_Click


    DoCmd.Quit

Exit_cmdExit_Click:
    Exit Sub

Err_cmdExit_Click:
    MsgBox Err.Description
    Resume Exit_cmdExit_Click

End Sub
Ryan Chong

I guess you need to pre-set the image size of ImageList5, so that when it loads the images into the listivew, the icons will look bigger..
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
pmathis

ASKER
I have it almost working except after I run cmdClearListView to reset the imagelist and reload images I get
                         "imagelist must be initialized before it can be used"

Private Sub cmdClearListView_Click()
      Me.ListView1.ListItems.Clear
    Set ListView1.Icons = Nothing
    Set ListView1.SmallIcons = Nothing
    imagelist6.ListImages.Clear
'Set ListView1.SmallIcons = imagelist6
'Set ListView1.Icons = imagelist6
End Sub

Private Sub cmdFillListView_Click()
    Call FillEmployees
End Sub

Private Sub Form_Load()
   ' Call FillEmployees
End Sub

Public Sub FillEmployees()
     
    'On Error GoTo ErrorHandler

    'set variables
    Dim rs As DAO.Recordset
    Dim db As Database
    Dim lstItem As ListItem
    Dim strSQL As String
  '---If I try this I get invalid object  >>>   Set ListView1.SmallIcons = imagelist6
 ' ---If I try this I get invalid object  >>>   Set ListView1.Icons = imagelist6
   
    Set db = CurrentDb()
    strSQL = "SELECT  * FROM listselections"
    Set rs = db.OpenRecordset(strSQL)

    With Me.ListView1
        'Set ListView style
        .View = lvwIcon
        'This is not supported by ListView 5
        .GridLines = True
        .FullRowSelect = True
        'Clear Header and ListItems
        .ListItems.Clear
        .ColumnHeaders.Clear
       
    End With
    'Set up column headers
    With Me.ListView1.ColumnHeaders
        .Add , , "Emp ID", 1000, lvwColumnLeft
        .Add , , "Salutation", 700, lvwColumnLeft
        .Add , , "Last Name", 2000, lvwColumnLeft
        .Add , , "First Name", 2000, lvwColumnLeft
        .Add , , "Hire Date", 1500, lvwColumnRight
    End With
    ' Add items and subitems to list control.
   
    rs.MoveFirst
    Do Until rs.EOF
    'load image path

       ' then set icon for each record
        'Set lstItem = Me.ListView1.ListItems.Add()
       
         imagelist6.ListImages.Add , , LoadPicture(rs!Text)
         Dim str As Integer
         str = rs!iid
     --this is where it errors out >>  Set lstItem = ListView1.ListItems.Add(, , "test", str, str)
        lstItem.Text = rs!iid
        'lstItem.Text = rs!employeeid
       ' lstItem.SubItems(1) = Nz(rs!TitleOfCourtesy, "N/A")
       ' lstItem.SubItems(2) = Nz(Trim(rs!LastName))
        'lstItem.SubItems(3) = Nz(Trim(rs!FirstName))
      '  lstItem.SubItems(4) = Format(rs!HireDate, "Medium Date")
       
        rs.MoveNext
    Loop
    'close recordset
    rs.Close
    Form.Requery
    DoCmd.Echo True
ErrorHandlerExit:
    Exit Sub
ErrorHandler:
    If Err = 3021 Then    ' no current record
        Resume Next
    Else
        MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
        Resume ErrorHandlerExit
    End If

End Sub

Ryan Chong

>> "imagelist must be initialized before it can be used"

Make sure you need to have at least an image inside your ImageList.
pmathis

ASKER
actually I figured out what the problem was, I was running the code in access and the when i
reset the image list and clear the listview

    ListView1.ListItems.Clear
    Set ListView1.SmallIcons = Nothing
   Set ListView1.Icons = Nothing
    imagelist6.ListImages.Clear
'and then tried to reinitialize
Set ListView1.Icons = imagelist6  'I would get "invalid object"
Set ListView1.Icons = imagelist6.object ' is what worked !


⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ryan Chong

>>and the when i reset the image list and clear the listview

yes, that was what I mentioned of:

>>Make sure you need to have at least an image inside your ImageList.

glad you found where the problem is.
pmathis

ASKER
I still have an issue when formatting the listview and I can't seem to make any progress.
I will be happy to give you all the points for this question if you can answer any of these questions.

1.)In the icon view of my listview I have spaces between icons, How do I remove or adjust the spacing?
2.)If an image exists the listview shows the image with text below it.
When in icon view if an image doesn't exist for a selection it just shows text, Is there any way to size the text selection so that it appears in place of the icon ?  I tried putting a blank image, but then there is just a blank image with text below it, what I would like to do is just put the text where the blank image appears.
ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.