Friday, June 19, 2020

Show Files List With Icons In Listview Using ImageList in VB6.0

Show Files List With Icons In Listview Using ImageList in VB6.0

In this post, we will learn items with image in Listview. Sometimes we require a list which contain image including items. The listview will use for this process due to its fast and easiest way in vb6.0. There is a simple process for doing practical on your VB6.0 application development. So, SKOTechLearn will explain the way to Show Files List with Icons in Listview using ImageList in VB6.0.

In the following steps we describe the 3 code process, in which you can use anyone code process for your desire.

Dynamically Add Items in ListView in VB6

Show files List with Path and Icon in Listview using ImageList in vb6.0 like bellow:
ListView_with_Files_and_Icons_in_VB6.0

Let’s find and learn, how can we find this type of output?

 (1). Create some images for Icons:
 We have to create some images for showing as icon in Listview. Now ,try to create through Paint with Print Screen process as I created bellow.

Images for Listview Icons in Vb6.0

 (2). Drag Some Controls:
 Now drag some controls like Command button for doing activity and Listview for Showing List with icons and Path and ImageList Control for fetching images.

Here we changed Listview1 name is MyLstView and ImageList1 name is MyImageList


Simply use ListView for Edit or Delete Selected Items


 (3) ImageList and Listview Settings: 
 First we add some image in ImageList through properties and set every image with Key name, which is use for calling inside Listview. After that we setting on Listview Properties like Column header and View type etc.

ImageList properties and ListView Properties Settings

In above given image you can Set Listview Properties with Column Header easily.


Now write code for listing Files with icon as describe bellow.

 Process(1):
 In this process we use Select Case Statement for showing files with path and icons in Listview.

VB6.0:
 Private Sub ShowFileIcon_Btn_Click()
 'First you have to set listview smallicon with ImageList
 Set MyLstView.SmallIcons = MyImageList
 Dim i As Integer
 i = 1
 With MyLstView

  Dim myFilesList As String
  'Use Dir for Collection files' List
  myFilesList = Dir("C:\SKOTechLearn_Files\*.*")
  'Clear Listview
  MyLstView.ListItems.Clear
  'Loop wntil Files' list will not Null or Empty
  While myFilesList > vbNullString

   Dim MyFLExten As String
   'Following formula is used for Extract "Extension" from Files
   MyFLExten = UCase(Mid(myFilesList, InStrRev(myFilesList, "."), Len(myFilesList)))
      
   Select Case MyFLExten
      Case ".JPG"
           ' Listview.ListItems.Ass [Index], [key], [Text],[Icon], [SmallIcon]
           .ListItems.Add , , myFilesList, , "ImgIco1"
      Case ".PNG"
              .ListItems.Add , , myFilesList, , "ImgIco1"
      Case ".XLS", ".XLSX"
              .ListItems.Add , , myFilesList, , "XlsIco6"
      Case ".PDF"
              .ListItems.Add , , myFilesList, , "PdfIco2"
      Case ".TXT"
              .ListItems.Add , , myFilesList, , "TxtIco4"
      Case ".XPS"
              .ListItems.Add , , myFilesList, , "XpsIco7"
      Case ".DOC", ".DOCX"
              .ListItems.Add , , myFilesList, , "DocIco5"
      Case Else
              .ListItems.Add , , myFilesList, , "OtherIco8"
    End Select

    .ListItems(i).ForeColor = vbBlue
    'List with Path and Files
    .ListItems(i).ListSubItems.Add , , "C:\SKOTechLearn_Files\" & myFilesList
    .ListItems(i).ListSubItems(1).ForeColor = vbRed
    .ListItems(i).ListSubItems.Add , , MyFLExten
    ' retun remaining Dir File's List
     myFilesList = Dir
    DoEvents
    i = i + 1
  Wend

 End With

 End Sub


Remove Multiple Checked Items from Listview Easily


 Process(2):
 In this Process we Use If Else Statement for showing Files List with icons in ListView.

VB6.0:
 Private Sub ShowFileIcon_Btn2_Click()
 'First you have to set listview smallicon with ImageList
 Set MyLstView.SmallIcons = MyImageList
 Dim i As Integer
 i = 1
 With MyLstView

   Dim MyFlList As String
  'Use Dir for Collection files' List From path
   MyFlList = Dir("C:\SKOTechLearn_Files\*.*")
   MyLstView.ListItems.Clear
   While MyFlList > vbNullString

      Dim MyFLExt As String
     'following formula used to extact "Extension" From Files
      MyFLExt = UCase(Mid(MyFlList, InStrRev(MyFlList, "."), Len(MyFlList)))

      If MyFLExt = ".JPG" Or MyFLExt = ".PNG" Or MyFLExt = ".XLS" Or MyFLExt = ".XLSX" Or MyFLExt = ".PDF" Or MyFLExt = ".TXT" Or MyFLExt = ".XPS" Or MyFLExt = ".DOC" Or MyFLExt = ".DOCX" Then

          If MyFLExt = ".JPG" Then
              ' Listview.ListItems.Ass [Index], [key], [Text],[Icon], [SmallIcon]
              .ListItems.Add , , MyFlList, , "ImgIco1"
          End If
          If MyFLExt = ".PNG" Then
              .ListItems.Add , , MyFlList, , "ImgIco1"
          End If
          If MyFLExt = ".XLS" Or MyExt = ".XLSX" Then
              .ListItems.Add , , MyFlList, , "XlsIco6"
          End If
          If MyFLExt = ".PDF" Then
              .ListItems.Add , , MyFlList, , "PdfIco2"
          End If
          If MyFLExt = ".TXT" Then
              .ListItems.Add , , MyFlList, , "TxtIco4"
          End If
          If MyFLExt = ".XPS" Then
              .ListItems.Add , , MyFlList, , "XpsIco7"
          End If
          If MyFLExt = ".DOC" Or MyExt = ".DOCX" Then
              .ListItems.Add , , MyFlList, , "DocIco5"
          End If
          
      Else
              .ListItems.Add , , MyFlList, , "OtherIco8"
      End If

      .ListItems(i).ForeColor = vbBlue
      .ListItems(i).ListSubItems.Add , , "C:\SKOTechLearn_Files\" & MyFlList
      .ListItems(i).ListSubItems(1).ForeColor = vbRed
      .ListItems(i).ListSubItems.Add , , MyFLExt
      ' retun remaining Dir File's List
       MyFlList = Dir
     DoEvents
     i = i + 1
   Wend

 End With
 End Sub


 Process(3):
In this process we use FileSystemObject for show Files in Listview with Icons.

How To Add FileSystemObject (scrrun.dll) in VB6.0?

For Using FileSystemObject, you have to first add references ‘Microsoft Scripting Runtime’. This reference contains FileSystemObject class.

The following image will show how to add Microsoft Scripting Runtime (scrrun.dll) in VB6.0.

  1. Go to Project menu and find “References..” option. 
  2. Click on this option and this will show “References..” window.
  3. This window will show the Available References List.
  4. Find “Microsoft Scripting Runtime” and check on it.

add FileSystemObject class in VB6.0
In this way you can add any Reference.

After adding Microsoft Scripting Runtime. Write following code.

VB6.0:
 Private Sub FilesListFSO_Cmd_Click()
 Dim myfsobj As New FileSystemObject
 Dim myfldr As Folder
 Dim myfl As File
 Set myfldr = myfsobj.GetFolder( "C:\SKOTechLearn_Files\" )
 'First you have to set listview smallicon with ImageList
 MyLstView.ListItems.Clear
 Set MyLstView.SmallIcons = MyImageList

 With MyLstView
   Dim li As Integer
   li = 1

   For Each myfl In myfldr.Files
      Dim MyFLExten As String
      'Extract Extension through FileSystemobject's GetExtensionName method
      MyFLExten = UCase(myfsobj.GetExtensionName(myfl))

      Select Case MyFLExten
          Case "JPG"
              .ListItems.Add , , myfl.Name, , "ImgIco1"
          Case "PNG"
              .ListItems.Add , , myfl.Name, , "ImgIco1"
          Case "XLS", "XLSX"
              .ListItems.Add , , myfl.Name, , "XlsIco6"
          Case "PDF"
              .ListItems.Add , , myfl.Name, , "PdfIco2"
          Case "TXT"
              .ListItems.Add , , myfl.Name, , "TxtIco4"
          Case "XPS"
              .ListItems.Add , , myfl.Name, , "XpsIco7"
          Case "DOC", "DOCX"
              .ListItems.Add , , myfl.Name, , "DocIco5"
          Case Else
              .ListItems.Add , , myfl.Name, , "OtherIco8"
      End Select

      .ListItems(li).ForeColor = vbBlue
      .ListItems(li).ListSubItems.Add , , myfl
      .ListItems(li).ListSubItems(1).ForeColor = vbRed
      .ListItems(li).ListSubItems.Add , , MyFLExten
      DoEvents
      li = li + 1

   Next
 End With
 'After show files list, clear Files Folders and FileSystemObject class
 Set myfl = Nothing
 Set myfldr = Nothing
 Set myfsobj = Nothing
 End Sub

Give above 3 process will show the same output as shown bellow image.
Listview With Icons, path and Filenames in Vb6.0
So, SKOTechLearn has Explained with some simple process to Show Files List With Icons In Listview Using ImageList in VB6.0.

How to move checked items from one Listview to Another?

0 comments: