Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I was wondering how I can add a image to the glistview on form_load. thanks!

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SetIcons()
pLoad_ListView()
End Sub

Private Sub pLoad_ListView()
Dim SQL As String
Dim n As Integer = 0
'PURPOSE: This procedure will fill the provided DataGrid
'with all the valid data in the system.
SQL = SELECT tblMenu_Items.ItemText as Item, tblMenu_Items.ItemDesc as Description, tblMenu_Items.form as Form1, tblMenu_Items.form2, tblMenu_Items.pandemic_cde as Code FROM tblMenu_Items WHERE tblMenu_Items.MenuID=1 AND tblMenu_Items.InActive=False;

Dim dr As OleDbDataReader
dr = myUsers.fdr(SQL)
n = dr.FieldCount
GListView1.Items.Clear()
For i As Integer = 0 To n - 1
GListView1.Columns.Add(dr.GetName(i).ToString(), 60)
Next
Do While dr.Read = True
Dim LI As New ListViewItem
LI.Text = String.Concat9'', dr(0), '')
LI.SubItems.Add(String.Concat'', dr(1),''))
LI.SubItems.Add(String.Concat'', dr(2),''))
LI.SubItems.Add(String.Concat('',dr(3),''))
LI.SubItems.Add(String.Concat('',dr(4),''))
GListView1.Items.Add(LI)
Loop

End Sub
Private Sub SetIcons()
'Set the Forms Icon to the Shell Help Icon
Dim hico As IntPtr()
ReDim hico(0)
ExtractSmallIconEx("shell32.dll", 23, IntPtr.Zero, hico, 1)
Me.Icon = Icon.FromHandle(hico(0))
DestroyIcon(hico(0))
'Fill our ImageList with Icons from the Shell.
ShellIcon(19, 9)
End Sub
Private Sub ShellIcon(ByVal StartIndex As Integer, ByVal qty As Integer)
'For Windows XP compatibility, we have to use an Imagelist
'set at 16 bit color to store the icon. This is because
'in the process of conversion from Icon/hIcon to bitmap
'the Alpha data for the image is lost and those shadows
'look ugly without Alpha.
Dim hico() As IntPtr = {New IntPtr}
ReDim hico(qty)
Call ExtractSmallIconEx("shell32.dll", StartIndex, IntPtr.Zero, hico, qty)
Dim i As Integer
For i = 0 To qty - 1
ImageList1.Images.Add(Icon.FromHandle(hico(i)))
Next
End Sub

Region " Shell Icon API "
<DllImport("shell32.dll", EntryPoint:="ExtractIconEx", CallingConvention:=CallingConvention.Cdecl)> _
Private Shared Function ExtractSmallIconEx _
(ByVal lpszFile As String, _
ByVal nIconIndex As Integer, _
ByVal phiconLarge As IntPtr, _
ByVal phiconSmall As IntPtr(), _
ByVal nIcons As Integer) As Boolean
End Function
<DllImport("user32", CallingConvention:=CallingConvention.Cdecl)> _
Private Shared Function DestroyIcon _
(ByVal hIcon As IntPtr) As Boolean
End Function
#End Region


thanks again
Posted

1 solution

First, learn how to use the pre tags to format your code...or even just the "code block" button on the form where you entered your answer.

Secondly, since gListView inherits ListView, it's the same as a ListView. You've already set up your ImageList which is the first step. Then, in the gListView properties, you have to set either LargeImageList or SmallImageList to that ImageList. (Which one you use depends on whether your View is set to LargeIcon or SmallIcon.) Though, the way you're adding it, you have to set the ListViewItem's ImageList to the ImageList Then, when you add the new item, add the ImageKey associated with that item.
VB
Dim LI As New ListViewItem
LI.Text = String.Concat('', dr(0), '')
LI.SubItems.Add(String.Concat('', dr(1),''))
LI.SubItems.Add(String.Concat('', dr(2),''))
LI.SubItems.Add(String.Concat('',dr(3),''))
LI.SubItems.Add(String.Concat('',dr(4),''))
LI.ImageList = ImageList1
LI.ImageKey = 5 'or whatever number it is
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900