Reputation:
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.
Upvotes: 5
Views: 41562
Reputation: 11
Yes, this is possible:
Dim imgList As New ImageList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListView1.View = View.Details
ListView1.Width = 500
ListView1.Columns.Add("Gender", 75, HorizontalAlignment.Left)
ListView1.Columns.Add("Name", 100, HorizontalAlignment.Left)
ListView1.Columns.Add("Notes", 350, HorizontalAlignment.Left)
ListView1.AllowColumnReorder = True
ListView1.Columns(0).DisplayIndex = 1
imgList.Images.Add("Male", Image.FromFile("C:\Users\Joe\Pictures\Male-Symbol.jpg"))
imgList.Images.Add("Female", Image.FromFile("C:\Users\Joe\Pictures\Female-Symbol.jpg"))
ListView1.SmallImageList = imgList
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim index As Integer
Select Case True
Case RadioButton1.Checked
index = 0
Case RadioButton2.Checked
index = 1
End Select
Dim lvi As New ListViewItem
lvi.ImageIndex = index
lvi.SubItems.Add(TextBox1.Text)
ListView1.Items.Add(lvi)
End Sub
Upvotes: 1
Reputation: 2076
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.
Upvotes: 9
Reputation: 15813
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)
...
Upvotes: 1