tags:

views:

894

answers:

2

I have sets of web hosted images that I need my user to be able to select 1 from each. I thought a listbox would work for this, but I can't figure out add an image to one. Is this possible? better way of doing this? I am using the latest free vb.

+1  A: 

Set ListBox1.DrawMode to DrawMode.OwnerDrawFixed or DrawMode.OwnerDrawVariable, and add a handler for drawing the images.

Private Sub listBox1_DrawItem(ByVal sender As System.Object, ByVal e As   System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim img As Image
img = sender.items(e.Index)
e.Graphics.DrawImage(img, targetsize) 
End Sub

You can add the images to the listbox items collection.

Dim img As Image
img = Image.FromFile("c:\tmp.jpg") ' or whatever
ListBox1.Items.Add(img)
...
xpda
+1  A: 

Use the Listview control instead, it provides better functionality, and doesn't suffer from an annoying resize bug. The listbox is carried over from VB6 days. The listview supports column headers, groupings and a bit more.

Add a Imagelist control to your form, to store the images; set it's ColorDepth property to 32-bit, and set the Listview's LargeImagelist property to the imagelist control you just added (this can all be done in code as well).

Add images to the Imagelist via this code:

ImageList1.Images.Add("imagekey", Image.FromStream(yourimagestream))

Add items to the Listview via this code:

ListView1.Items.Add("list item title", "imagekey")

The "imagekey" is a way to tell the Listview which image to use. You can also use indexes for icons, but specifying an index that doesn't exist will give an index out of range exception, whereas a key that doesn't exist, will just use no image instead.

Oh you also want to set the Listview Multiselect property to False (if you only want them to select one at a time), and access the SelectedIndexChanged() and ItemActivate() events for when the user clicks / double-clicks on items respectively.

Wez