Results 1 to 3 of 3

Thread: ComboBox with pictures

  1. #1

    Thread Starter
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    ComboBox with pictures

    Following the post of .Paul. here :https://www.vbforums.com/showthread....=1#post5495190

    I decided to adapt his code to have a ComboBox with a list of items associated to some pictures.

    As example of application, I got inspired by the latest contest thread and decide to show some dices in the combobox.

    Here is the code of the class :

    VB.net 2010 Code:
    1. Public Class Pic_combobox
    2.     Inherits ComboBox
    3.  
    4. 'this class is for the example, can be replaced by anything and put outside the combobox class , it is here for convenience only
    5. Public Class Dice
    6.    Property Name As String = ""
    7.    Property Picture As Image = Nothing
    8.    Property Value As Integer = 0
    9.  
    10.    Public Overrides Function ToString() As String
    11.     Return Name
    12.    End Function
    13.     End Class
    14.  
    15. ' the list of items to put in the combobox, same can be put outside the class, it is here for convenience only
    16.    Public list_dice As New List(Of Dice) From {
    17.     New Dice With {.Name = "D4", .Value = 4, .Picture = My.Resources.D4},
    18.     New Dice With {.Name = "D6", .Value = 6, .Picture = My.Resources.D6},
    19.     New Dice With {.Name = "D8", .Value = 8, .Picture = My.Resources.D8},
    20.     New Dice With {.Name = "D10", .Value = 10, .Picture = My.Resources.D10},
    21.     New Dice With {.Name = "D12", .Value = 12, .Picture = My.Resources.D12},
    22.     New Dice With {.Name = "D20", .Value = 20, .Picture = My.Resources.D20},
    23.     New Dice With {.Name = "D100", .Value = 100, .Picture = My.Resources.D100}
    24.     }
    25.  
    26.  
    27.     Public Sub New()
    28.     Me.DropDownStyle = ComboBoxStyle.DropDownList
    29.     Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
    30.     MyBase.Items.Clear()
    31.     MyBase.Items.AddRange(list_dice.ToArray)
    32. End Sub
    33.  
    34.     Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
    35.       'when control is created with designer, it adds 2 times the items, this will remove them
    36.       ' if the control is created via code, it works correctly and the following code is not needed
    37.       If MyBase.Items.Count > 7 Then
    38.          For i = MyBase.Items.Count - 1 To 7 Step -1
    39.             MyBase.Items.RemoveAt(i)
    40.          Next
    41.       End If
    42.  
    43.       Dim d As New Dice With {.Name = "", .Value = 0, .Picture = My.Resources.D100}
    44.       If e.Index <> -1 Then
    45.          d = list_dice(e.Index)
    46.       End If
    47.       If (e.State And DrawItemState.ComboBoxEdit) = DrawItemState.ComboBoxEdit Then
    48.          ' Draw the contents of the edit area of the combo box.
    49.          e.Graphics.DrawImage(d.Picture, New Point(2, e.Bounds.Top + 2))
    50.          e.Graphics.DrawString(If(d.Name = Nothing, "Empty", d.Name), Me.Font, Brushes.Black, 65, e.Bounds.Top + 22)
    51.       Else
    52.          '  Draw the contents of an item in the drop down list.
    53.          e.Graphics.DrawImage(d.Picture, New Point(2, e.Bounds.Top + 2))
    54.          e.Graphics.DrawString(d.Name, Me.Font, Brushes.Black, 65, e.Bounds.Top + 22)
    55.          ' Draw the focus rectangle.
    56.          e.DrawFocusRectangle()
    57.       End If
    58.       MyBase.OnDrawItem(e)
    59.    End Sub
    60.  End Class


    in the form1, for the example, I put with the designer a pic_combobox and I create a second one during the form.load event
    VB.net 2010 Code:
    1. Public Class Form1
    2.  
    3.    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    4.       Dim cb As New Pic_combobox With {.Location = New Point(40, 40), .ItemHeight = 60, .Width = 150}
    5.       Me.Controls.Add(cb)
    6.    End Sub
    7.  
    8. End Class

    The itemHeight of the ComboBox must be adapted to the size of the picture as the position of the text.

    here is the result :
    Name:  picture_combobox.png
Views: 974
Size:  44.0 KB

    and here is the project including the custom control
    Picture_combobox.zip

    I have 2 issues to solve (I solved one but not in an elegant manner).
    the first issue is : when I put the control on the form with the designer, the items are added 2 times and I don't know why. It doesn't occur when I create the control with code (in the load event of form1 for example).

    I solved that with with the following code in the OnDrawItem sub but it not very elegant :
    Code:
    If MyBase.Items.Count > 7 Then
             For i = MyBase.Items.Count - 1 To 7 Step -1
                MyBase.Items.RemoveAt(i)
             Next
          End If
    The second issue is the e.DrawFocusRectangle(). It draws the rectangle on VB2017 but not on VB2010 and when the rectangle is drawn, it doesn't disappear fully when the cursor goes on an other item. The text becomes also bold and doesn't come back to normal. It is not critical as it is only some display problem but it is not nice.
    Last edited by Delaney; Oct 1st, 2020 at 08:03 AM. Reason: typo
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: ComboBox with pictures

    Ungh... gonna be pedantic for a moment... but that's not a d100 there... that's the d% die... or percent die...
    a d100 truly is a 100-sided die:



    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: ComboBox with pictures

    Georgia guy can be playful sometimes, you know it was just for the example, don't you ?

    I will change the name for "green funny dice with 10 faces"

    by the way, have a look :https://www.vbforums.com/showthread....=1#post5495421
    Last edited by Delaney; Oct 1st, 2020 at 10:01 AM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

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