-
Apr 13th, 2012, 02:04 PM
#1
Thread Starter
New Member
[RESOLVED] what is wrong with the code about inserting icons into listview ?
ListView1.SmallIcons = Nothing
ListView1.Icons = Nothing
ImageList1.ListImages.Clear
ImageList2.ListImages.Clear
'------------------------------------- ' bitmap files to the listbox1
Dim file As String
file = Dir$("C:\Program Files\folder\*.*")
Do While Len(file)
List1.AddItem (Mid(file, 1, InStr(file, ".") - 1)) & ".bmp"
file = Dir$()
Loop
'------------------------------------------'only file names to the listbox2 for imagelist2 items keys
Dim filename As String
filename = Dir$("C:\Program Files\folder\*.*")
Do While Len(filename)
List2.AddItem (Mid(filename, 1, InStr(filename, ".") - 1))
filename = Dir$()
Loop
'------------------------------ insert images into the imagelist
Dim i As Integer
Dim imgy As ListImage
For i = 0 To List1.ListCount - 1
Set imgy = ImageList2.ListImages.Add(, List2.List(i), LoadPicture("C:\Program Files\folder" & "\" & List1.List(i)))
Next i
'-------------------------------------
ListView1.SmallIcons = ImageList1
ListView1.Icons = ImageList2
'-------------------- insert datas to the listview with imagelist icons
Dim lst As ListItem
Dim X As Integer
If rst.RecordCount = 0 Then Exit Sub
Dim s As Long
For s = 1 To rst.EOF
Set lst = ListView1.ListItems.Add(, , rst.Fields(0), ImageList2.ListImages.Item(s).Picture)
ListView1.ListItems.Item(s).Text = ImageList2.ListImages.Item(s).Key
ListView1.ListItems.Item(s).Icon = s
For X = 1 To rst.Fields.Count - 1
If Not IsNull(rst(X)) Then
lst.SubItems(X) = rst(X)
Else
lst.SubItems(X) = ""
End If
Next X
rst.MoveNext
Next s
-
Apr 13th, 2012, 02:07 PM
#2
Re: what is wrong with the code about inserting icons into listview ?
Maybe you can tell us? What errors are you getting, what is the problem exactly?
-
Apr 13th, 2012, 02:11 PM
#3
Thread Starter
New Member
Re: what is wrong with the code about inserting icons into listview ?
when i add these codes , list view is empty
Set lst = ListView1.ListItems.Add(, , rst.Fields(0), ImageList2.ListImages.Item(s).Picture)
ListView1.ListItems.Item(s).Text = ImageList2.ListImages.Item(s).Key
ListView1.ListItems.Item(s).Icon = s
-
Apr 13th, 2012, 03:05 PM
#4
Re: what is wrong with the code about inserting icons into listview ?
You assign the Index of the imagelist picture you want for the listview, not the picture itself
So, instead of
Code:
Set lst = ListView1.ListItems.Add(, , rst.Fields(0), ImageList2.ListImages.Item(s).Picture)
Maybe this
Code:
Set lst = ListView1.ListItems.Add(, , rst.Fields(0), ImageList2.ListImages.Item(s).Key)
Sorry, I don't have my VB running on this machine, so I cannot test the suggestion. Hope all works ok, if not let us know
-
Apr 13th, 2012, 05:54 PM
#5
Thread Starter
New Member
Re: what is wrong with the code about inserting icons into listview ?
hi i changed some codes ,it worked but when i click the listview there is an error
runtime error 35602 "key is not unique in collection " the new code is :
ListView1.SmallIcons = Nothing
ListView1.Icons = Nothing
ImageList2.ListImages.Clear
Dim file As String
file = Dir$("C:\Program Files\folder\*.*")
Do While Len(file)
List1.AddItem (Mid(file, 1, InStr(file, ".") - 1)) & ".bmp"
file = Dir$()
Loop
Dim filename As String
filename = Dir$("C:\Program Files\folder\*.*")
Do While Len(filename)
List2.AddItem (Mid(filename, 1, InStr(filename, ".") - 1))
filename = Dir$()
Loop
Dim i As Integer
Dim imgy As ListImage
For i = 0 To List1.ListCount - 1
Set imgy = ImageList2.ListImages.Add(, List2.List(i), LoadPicture("C:\Program Files\folder" & "\" & List1.List(i)))
Next i <<< error message here >>>
'------------------------
ListView1.SmallIcons = ImageList2
Dim lst As ListItem
Dim X As Integer
If rst.RecordCount = 0 Then Exit Sub
Do Until rst.EOF
Set lst = ListView1.ListItems.Add(, , rst.Fields(0))
For X = 1 To rst.Fields.Count - 1
If Not IsNull(rst(X)) Then
lst.SubItems(X) = rst(X)
Else
lst.SubItems(X) = ""
End If
Next X
rst.MoveNext
Loop
Dim t As Integer
Dim m As Integer
For t = 1 To ListView1.ListItems.Count
For m = 1 To ImageList2.ListImages.Count
If ListView1.ListItems.Item(t).Text = ImageList2.ListImages.Item(m).Key Then
ListView1.ListItems.Item(t).SmallIcon = ImageList2.ListImages.Item(m).Index
End If
Next m
Next t
-
Apr 15th, 2012, 10:17 AM
#6
Thread Starter
New Member
Re: what is wrong with the code about inserting icons into listview ?
i have finally resolved the problem
' add listview(1),filelist (1),listbox(2),imagelist(1),progressbar(1)
' and listview's first column datas must have same name with bitmap images' names in the folder
' there was a problem with listview loaded twice, for this not to be,write " If form1(where listview in).Visible = True Then " into form_activate sub
'after the database connection
'set progressbar1.visible = false before the code sub
'into the sub :
On Error Resume Next
File1.Path = "C:\Program Files\foldername" 'foldername where the bitmap files in
Dim i As Integer
For i = 0 To File1.ListCount - 1
List1.AddItem File1.Path & "\" & File1.List(i) 'add images paths to the list1 for imagelist pictures
Next i
Dim p As Integer
For p = 0 To File1.ListCount - 1
File1.ListIndex = p
List2.AddItem (Left$(File1.FileName, Len(File1.FileName) - 4)) 'add image names to the list2 for imagelist keys
Next p
Dim l As Integer
For l = 1 To File1.ListCount
ImageList1.ListImages.Add Index:=l, Key:=List2.List(l - 1), Picture:=LoadPicture(List1.List(l - 1)) 'add images to imagelist1
Next l
Listview1.Sorted = False ' when i add these code ,listview is more faster
ListView1.ColumnHeaders.Clear
ListView1.ListItems.Clear
ProgressBar1.Min = 0
ProgressBar1.Max = rst.RecordCount
ProgressBar1.Value = 0
ListView1.ColumnHeaders.Add , , "header1", 3000 'add columnheaders
ListView1.ColumnHeaders.Add , , "header2", 1700
'and the other columnheaders
Dim lst As ListItem
Dim X As Integer
If rst.RecordCount = 0 Then Exit Sub
Dim t As Integer
Dim m As Integer
For t = 0 To rst.RecordCount
ProgressBar1.Visible = True
Set ListView1.SmallIcons = ImageList1
Set lst = ListView1.ListItems.Add(, , rst.Fields(0)) ' add first field datas to listview1
For m = 1 To ImageList1.ListImages.Count ' check if the listview first columns items text is same with imagelist images keys,add icons
If ListView1.ListItems.Item(t).Text = ImageList1.ListImages.Item(m).Key Then ListView1.ListItems.Item(t).SmallIcon = ImageList1.ListImages.Item(m).Index
Next m
For X = 1 To rst.Fields.Count - 1 'add other subitems
If Not IsNull(rst(X)) Then
lst.SubItems(X) = rst(X)
Else
lst.SubItems(X) = ""
End If
Next X
rst.MoveNext
ProgressBar1.Value = ProgressBar1.Value + 1
Next t
ProgressBar1.Value = 0
ProgressBar1.Visible = False
ListView1.SetFocus
Last edited by ozgurdunya; Apr 17th, 2012 at 02:04 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|