Visual Basic > Visual Basic Forums > Visual Basic General > How can i show pictures in listbox?
Ask a questionAsk a question
 

AnswerHow can i show pictures in listbox?

  • Friday, November 06, 2009 10:44 PMmausmust Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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 ?

Answers

  • Saturday, November 07, 2009 7:36 AMjwavila Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
    • Marked As Answer bymausmust Saturday, November 07, 2009 12:38 PM
    •  
  • Saturday, November 07, 2009 12:38 PMmausmust Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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...
    • Marked As Answer bymausmust Saturday, November 07, 2009 12:38 PM
    •  

All Replies

  • Friday, November 06, 2009 11:37 PMMalange Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
  • Saturday, November 07, 2009 5:00 AMDoug__ Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Saturday, November 07, 2009 7:36 AMjwavila Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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
    • Marked As Answer bymausmust Saturday, November 07, 2009 12:38 PM
    •  
  • Saturday, November 07, 2009 10:21 AMmausmust Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?
  • Saturday, November 07, 2009 11:38 AMParamu Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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
  • Saturday, November 07, 2009 11:54 AMmausmust Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
    .                          .
    .                          .
    .                          .


  • Saturday, November 07, 2009 12:38 PMmausmust Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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...
    • Marked As Answer bymausmust Saturday, November 07, 2009 12:38 PM
    •  
  • Saturday, November 07, 2009 3:53 PMjwavila Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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
    

  • Sunday, November 08, 2009 2:27 AM--Steve-- Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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!