Microsoft Developer Network >
Página principal de foros
>
Visual Basic General
>
How can i show pictures in listbox?
How can i show pictures in listbox?
- I have a listbox and there will be names in it.I want to display gender type icons in each line near peoples names. How can i do that in vb.net 2008 ?
Respuestas
- As Doug said, best to use a ListView
Create an imagelist with your gender type images
When adding items to the listview, use the index of the imagelist to add the appropriate image
note: the images are added to the first column (must be added as the listview Item, not a SubItem). But you can switch the order of the columns using ListView.Columns.DisplayIndex to move the image column over if you want it like Name Gender- Marcado como respuestamausmust sábado, 07 de noviembre de 2009 12:38
- Ok i solved it :D Commands i used:
Dim item1 As New ListViewItem("", 0)
item1.SubItems.Add("man1")
ListView1.Items.Add(item1)
and there is no ListView.Columns.DisplayIndex command so i just changed it in listview1's properties.
Thanx everybody...- Marcado como respuestamausmust sábado, 07 de noviembre de 2009 12:38
Todas las respuestas
- have a look here:
http://social.msdn.microsoft.com/Search/en-US/?query=picture%20in%20listbox&rq=meta:Search.MSForums.ForumID(0f60fa48-1ceb-41ee-a10a-0dfcee7e19bd)&rn=Visual+Basic+General+Forum
Don't judge me, just Upgrade me. Thanks! - I'm not aware of any simple way to do what you ask with the ListBox. It might be possible to do by drawing each of the items in the ListBox yourself (OwnerDraw). The ListView control is already designed to display icons with each item. You should look into using it instead of the ListBox.
:)
Doug
SEARCH ... then ask - As Doug said, best to use a ListView
Create an imagelist with your gender type images
When adding items to the listview, use the index of the imagelist to add the appropriate image
note: the images are added to the first column (must be added as the listview Item, not a SubItem). But you can switch the order of the columns using ListView.Columns.DisplayIndex to move the image column over if you want it like Name Gender- Marcado como respuestamausmust sábado, 07 de noviembre de 2009 12:38
- Thanks for your answers.One thing i could't do is there is no ListView.Columns.DisplayIndex command? How can i change image column with name column?
Dim ImageListSmall As New ImageList()
ImageListSmall.Images.Add(Bitmap.FromFile("C:\MausMust\Item_desc.gif"))
ImageListSmall.Images.Add(Bitmap.FromFile("C:\MausMust\spec.gif"))
ImageListSmall.Images.Add(Bitmap.FromFile("C:\MausMust\Item_spci.gif"))ListView1.Columns.Add("ITEM_DESCRIPTION", k1 + k2, HorizontalAlignment.Left)
ListView1.Columns.Add("ITEM_CODE", k2, HorizontalAlignment.Left)
ListView1.Columns.Add("ITEM_SPECIFICATION", k1, HorizontalAlignment.Left)
ListView1.SmallImageList = ImageListSmall
ListView1.Columns(0).ImageIndex = 0
ListView1.Columns(1).ImageIndex = 1
ListView1.Columns(2).ImageIndex = 2
ListView1.Font = New System.Drawing.Font("Times New Roman", 8, FontStyle.Regular, GraphicsUnit.Point, Byte.MinValue)
U.PARANTHAMAN- Thats cool but not solves my problem.In your description, there are icons in headers but what i want to do is change icon column with names column.Like that;
People | Gender type icon
Man1 | male icon
Woman1 | female icon
. .
. .
. .
Because when i insert items and icons to my listview, preview is just like that ;
People | Gender type icon
male icon | man1
female icon | woman1
. .
. .
. .
- Ok i solved it :D Commands i used:
Dim item1 As New ListViewItem("", 0)
item1.SubItems.Add("man1")
ListView1.Items.Add(item1)
and there is no ListView.Columns.DisplayIndex command so i just changed it in listview1's properties.
Thanx everybody...- Marcado como respuestamausmust sábado, 07 de noviembre de 2009 12:38
- Hi mausmust
this thread has been marked as answered, but in case you look at it again...
because the icon will add to the "first" column, I set up my columns with the Gender column (to show the icon) first, but then tell it to Display that column second (index of 1)
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
- You take Doug's advice and use the ListView and then don't give him credit for an answer. That's a real slap to his face! Give him credit (mark as an answer) for pointing you to the correct control to use!

