System icons in Listview

  • Thread starter Thread starter Ty
  • Start date Start date
T

Ty

I am using the following code to add the system associated icons to a
listview and seem to be having trouble.

When the project is run it seems like there should be an icon as the
text is moved to the right enough for where the icon would be, but
there is no icon showing.

I'm not sure if this is a Vista issue of a listview setup problem. I
am also getting an error when there is no icon associated with an
item.

CODE to get the icons.
Private Sub AddImages(ByVal strFileName As String)

Dim shInfo As SHFILEINFO
shInfo = New SHFILEINFO()
shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
shInfo.szTypeName = New String(vbNullChar, 80)
Dim hIcon As IntPtr
hIcon = SHGetFileInfo(strFileName, 0, shInfo, Marshal.SizeOf
(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
Dim MyIcon As System.Drawing.Icon
MyIcon = System.Drawing.Icon.FromHandle(shInfo.hIcon)
ImageList1.Images.Add(strFileName.ToString(), MyIcon)

End Sub

CALLING PROCDURE
Private Sub GetFiles(ByVal sender As Object, ByVal e As
DirectorySelectedEventArgs) Handles dtEvent.DirectorySelected

Dim fName As String 'The file name to get the icon from.
Dim FileExtension As String
Dim SubItemIndex As Integer
Dim DateMod As String
Dim strFileName As String

ListView1.Clear()
fName = e.DirectoryName

'Set up the ListView
ListView1.Columns.Add("File Name", 300,
HorizontalAlignment.Left)
ListView1.Columns.Add("File Type", 80,
HorizontalAlignment.Left)
ListView1.Columns.Add("Date Modified", 150,
HorizontalAlignment.Left)

For Each file As String In IO.Directory.GetFiles(fName)
strFileName = file.Substring(file.LastIndexOf("\"c) + 1)
ListView1.Items.Add(strFileName)
FileExtension = IO.Path.GetExtension(file)
DateMod = IO.File.GetLastWriteTime(file).ToString()
AddImages(file)
ListView1.Items(SubItemIndex).SubItems.Add
(FileExtension.ToString() & " File")
ListView1.Items(SubItemIndex).SubItems.Add(DateMod.ToString
())
SubItemIndex += 1
Next

End Sub
 
Ty said:
I am using the following code to add the system associated icons to a
listview and seem to be having trouble.

When the project is run it seems like there should be an icon as the
text is moved to the right enough for where the icon would be, but
there is no icon showing.

I'm not sure if this is a Vista issue of a listview setup problem. I
am also getting an error when there is no icon associated with an
item.

CODE to get the icons.
Private Sub AddImages(ByVal strFileName As String)

Dim shInfo As SHFILEINFO
shInfo = New SHFILEINFO()
shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
shInfo.szTypeName = New String(vbNullChar, 80)
Dim hIcon As IntPtr
hIcon = SHGetFileInfo(strFileName, 0, shInfo, Marshal.SizeOf
(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
Dim MyIcon As System.Drawing.Icon
MyIcon = System.Drawing.Icon.FromHandle(shInfo.hIcon)
ImageList1.Images.Add(strFileName.ToString(), MyIcon)

End Sub

CALLING PROCDURE
Private Sub GetFiles(ByVal sender As Object, ByVal e As
DirectorySelectedEventArgs) Handles dtEvent.DirectorySelected

Dim fName As String 'The file name to get the icon from.
Dim FileExtension As String
Dim SubItemIndex As Integer
Dim DateMod As String
Dim strFileName As String

ListView1.Clear()
fName = e.DirectoryName

'Set up the ListView
ListView1.Columns.Add("File Name", 300,
HorizontalAlignment.Left)
ListView1.Columns.Add("File Type", 80,
HorizontalAlignment.Left)
ListView1.Columns.Add("Date Modified", 150,
HorizontalAlignment.Left)

For Each file As String In IO.Directory.GetFiles(fName)
strFileName = file.Substring(file.LastIndexOf("\"c) + 1)
ListView1.Items.Add(strFileName)
FileExtension = IO.Path.GetExtension(file)
DateMod = IO.File.GetLastWriteTime(file).ToString()
AddImages(file)
ListView1.Items(SubItemIndex).SubItems.Add
(FileExtension.ToString() & " File")
ListView1.Items(SubItemIndex).SubItems.Add(DateMod.ToString
())
SubItemIndex += 1
Next

End Sub

The name 'SubItemIndex' is confusing because it's actually the index of the
item, not the index of the sub item. However, I don't see where you assign
an icon to the item in the listview. I only see that Icons are added to an
imagelist in Sub AddImages.

BTW, is Icon.ExtractAssociatedIcon an option?


Armin
 
Ty said:
I am using the following code to add the system associated icons to a
listview and seem to be having trouble.

When the project is run it seems like there should be an icon as the
text is moved to the right enough for where the icon would be, but
there is no icon showing.

I'm not sure if this is a Vista issue of a listview setup problem. I
am also getting an error when there is no icon associated with an
item.

CODE to get the icons.
Private Sub AddImages(ByVal strFileName As String)

Dim shInfo As SHFILEINFO
shInfo = New SHFILEINFO()
shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
shInfo.szTypeName = New String(vbNullChar, 80)
Dim hIcon As IntPtr
hIcon = SHGetFileInfo(strFileName, 0, shInfo, Marshal.SizeOf
(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
Dim MyIcon As System.Drawing.Icon
MyIcon = System.Drawing.Icon.FromHandle(shInfo.hIcon)
ImageList1.Images.Add(strFileName.ToString(), MyIcon)

End Sub

CALLING PROCDURE
Private Sub GetFiles(ByVal sender As Object, ByVal e As
DirectorySelectedEventArgs) Handles dtEvent.DirectorySelected

Dim fName As String 'The file name to get the icon from.
Dim FileExtension As String
Dim SubItemIndex As Integer
Dim DateMod As String
Dim strFileName As String

ListView1.Clear()
fName = e.DirectoryName

'Set up the ListView
ListView1.Columns.Add("File Name", 300,
HorizontalAlignment.Left)
ListView1.Columns.Add("File Type", 80,
HorizontalAlignment.Left)
ListView1.Columns.Add("Date Modified", 150,
HorizontalAlignment.Left)

For Each file As String In IO.Directory.GetFiles(fName)
strFileName = file.Substring(file.LastIndexOf("\"c) + 1)
ListView1.Items.Add(strFileName)
FileExtension = IO.Path.GetExtension(file)
DateMod = IO.File.GetLastWriteTime(file).ToString()
AddImages(file)
ListView1.Items(SubItemIndex).SubItems.Add
(FileExtension.ToString() & " File")
ListView1.Items(SubItemIndex).SubItems.Add(DateMod.ToString
())
SubItemIndex += 1
Next

End Sub

The name 'SubItemIndex' is confusing because it's actually the index of the
item, not the index of the sub item. However, I don't see where you assign
an icon to the item in the listview. I only see that Icons are added to an
imagelist in Sub AddImages.

BTW, is Icon.ExtractAssociatedIcon an option?


Armin
 
The name 'SubItemIndex' is confusing because it's actually the index of the
item, not the index of the sub item. However, I don't see where you assign
an icon to the item in the listview. I only see that Icons are added to an
imagelist in Sub AddImages.

BTW, is Icon.ExtractAssociatedIcon an option?

Armin- Hide quoted text -

- Show quoted text -

The Listview is assigned to the image control in the Load event.

Not sure about the ExtratAssociatedIcon.

Ty
 
The name 'SubItemIndex' is confusing because it's actually the index of the
item, not the index of the sub item. However, I don't see where you assign
an icon to the item in the listview. I only see that Icons are added to an
imagelist in Sub AddImages.

BTW, is Icon.ExtractAssociatedIcon an option?

Armin- Hide quoted text -

- Show quoted text -

The Listview is assigned to the image control in the Load event.

Not sure about the ExtratAssociatedIcon.

Ty
 
I got it figured out. I was not assigning the icon to the item added
to the listview in the FOR EACH loop.

I changed this line to ListView1.Items.Add(strFileName, imgindex)

and then this at the end to increase the index right above the 'Next'
statement... imgindex = imgindex + 1

Works like a champ now.

Ty
 
I got it figured out. I was not assigning the icon to the item added
to the listview in the FOR EACH loop.

I changed this line to ListView1.Items.Add(strFileName, imgindex)

and then this at the end to increase the index right above the 'Next'
statement... imgindex = imgindex + 1

Works like a champ now.

Ty
 
Back
Top