|
-
Nov 11th, 2007, 08:47 PM
#1
Thread Starter
Frenzied Member
[2005] Getting the images in a imagelist
Is there anyway to get the image names that are currently in a listview?
-
Nov 11th, 2007, 09:42 PM
#2
Re: [2005] Getting the images in a imagelist
What do you mean by "image names"? The Image class doesn't have a Name property so Image objects don't have names.
If you mean the name of the files the images were created from then you're out of luck because there's no accessible connection between an Image object and the original file. Of course there's not, because Image objects don't have to come from a file at all.
If you actually mean the key it was added to the imageList with, then that would be the ImageList.Images.Keys property, which the MSDN documentation would have told you.
-
Nov 13th, 2007, 03:26 AM
#3
Thread Starter
Frenzied Member
Re: [2005] Getting the images in a imagelist
Hm well maybe you can help me out with my problem. Im making a slideshow where the user can add, delete and view pictures. The pictures are contained in a string, for example C:\Pic1|C:\Pic2|C:\stuff\Pic3 ect. My question is how could i use the code below but replace the s with the picture name and then remove the picture from the string?
vb Code:
dim pic as string = "C:\Pic1|C:\Pic2|C:\stuff\Pic3"
split1 = pic.Split("|")
For Each str As String In split1
If str.Trim() <> "" Then
ImageList1.Images.Add(Image.FromFile(str))
End If
Next str
For i As Integer = 0 To ImageList1.Images.Count - 1
ListView1.Items.Add("s", i)
Next
-
Nov 13th, 2007, 03:55 AM
#4
Re: [2005] Getting the images in a imagelist
You should only have one loop for a start. There's nothing to stop you adding the Image to the ImageList and the item to the ListView at the same time. You should:
1. Get the file path.
2. Create the Image.
3. Add the Image to the ImageList WITH a key.
4. Create ListViewItem.
5. Set its ImageKey.
6. Add the item to the ListView.
All that should be inside one loop.
-
Nov 13th, 2007, 04:33 AM
#5
Thread Starter
Frenzied Member
Re: [2005] Getting the images in a imagelist
Ive been playing around with it and this is what ive come up with so far
vb Code:
dim pic as string = "C:\Pic1|C:\Pic2|C:\stuff\Pic3"
split1 = pic.Split("|")
For Each str As String In split1
If str.Trim() <> "" Then
ImageList1.Images.Add(id, Image.FromFile(str))
split1(id) = split1(id).Substring(InStrRev(split1(id), "\"))
ListView1.Items.Add(split1(id), id)
End If
If id < split1.Length Then
id += 1
End If
Next str
Next str
Last edited by Motoxpro; Nov 13th, 2007 at 04:41 AM.
-
Nov 13th, 2007, 05:18 AM
#6
Re: [2005] Getting the images in a imagelist
Wouldn't you just use the file paths as the keys? If you're just going to use numbers then the keys are pointless and you may as well just use indexes.
Also, I have big issue with variable names like "split1" when something like "fileNames" is obviously far more descriptive of the variable's purpose.
vb.net Code:
Dim fileNames As String() = pathString.Spit("|"c)
Dim fileName As String
Dim img As Image
For index As Integer = 0 To fileNames.GetUpperBound(0)
fileName = fileNames(index)
img = Image.FromFile(fileName)
ImageList1.Images.Add(fileName, img)
ListView1.Items.Add(IO.Path.GetFileName(fileName), fileName)
Next index
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
|