Results 1 to 7 of 7

Thread: [2005] Displaying Images in a ListBox

  1. #1

    Thread Starter
    Lively Member Underworld1020's Avatar
    Join Date
    Jun 2006
    Location
    NY, USA
    Posts
    99

    [2005] Displaying Images in a ListBox

    I've been working on this for about an hour now and I've almost got it. So far I can draw both images and strings in a ListBox, but there's a problem. I can't get the text to appear above the images. Here's the code I have so far:

    Code:
        Private Sub outputListBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles outputListBox.DrawItem
    
            Dim drawString As String = outputListBox.Items.Item(e.Index).ToString
            Dim drawFont As New Font("Times New Roman", 10)
            Dim drawBrush As New SolidBrush(Color.Black)
            Dim drawPoint As New PointF(e.Bounds.Left, e.Bounds.Top)
    
            e.Graphics.DrawImage(My.Resources.background, New PointF(0.0F, 0.0F))
            e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint)
        End Sub
    Does anyone know how I can get the text to appear above the image?
    Thanks in advance.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Displaying Images in a ListBox

    you are drawing the image at point 0,0 which is the upper left corner of the item bounds.

    You need to increase the Y param (up and down) of your New PointF() when you call the DrawImage so that it will be drawn at a Y offset of a certain number. You will have to play around for a minute to determine what that number is.

    Also if you find that the text/image is being cut off when you do this, then set the ItemHeight property of the listbox to a higher value in the property grid at design time. This property is only used when OwnerDrawing the listbox, and it tells the listbox exactly how tall each item should be in the listbox.

  3. #3

    Thread Starter
    Lively Member Underworld1020's Avatar
    Join Date
    Jun 2006
    Location
    NY, USA
    Posts
    99

    Re: [2005] Displaying Images in a ListBox

    I've seen how to that stuff around the web, but what I want to do is make the image a background image, so it appears behind the text.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Displaying Images in a ListBox

    oh... your original post said ABOVE.. which means something quite different than BEHIND...

    Are you trying to have one big background image for the listbox? or does each item in the listbox have its own background image? If its the first one, I think the problem you are going to run into is that the drawitem event is exactly that, for drawing individual items as needed, not to draw the entire listbox...So if you are drawing a full background image in EVERY call to the DrawItem handler, what you end up doing is painting over what is already there.

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Displaying Images in a ListBox

    another option you can use is to use a listview, and by setting a few properties correctly on it, it can act just like a listbox (or better)

    the attached screenshot is a listview, notice how it looks just like a listbox.
    Attached Images Attached Images  

  6. #6

    Thread Starter
    Lively Member Underworld1020's Avatar
    Join Date
    Jun 2006
    Location
    NY, USA
    Posts
    99

    Re: [2005] Displaying Images in a ListBox

    I want one big backgorund image for the ListBox.

    I've heard that a ListView is easier, but I'm doing this for a my visual basic class, and it says to use a ListBox control. Otherwise I would go with a ListView control.

    The example image of the project handed out to me has a ListBox with an image in the backgound of it. Although the example running program doesn't at all, but I thought it would be cool to make it actually have one. Plus it would give me some more experience.

    So I would like to use a ListBox control if I can, even though it isn't the smart way to go.
    What handler do you think I should use?
    Last edited by Underworld1020; Apr 4th, 2007 at 05:49 PM.

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] Displaying Images in a ListBox

    The issue is that the listbox simply does NOT have a background image property...

    So without doing a bunch of custom drawing there is no easy way to just set a picture as the background.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width