-
Oct 1st, 2020, 07:32 AM
#1
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:
Public Class Pic_combobox Inherits ComboBox 'this class is for the example, can be replaced by anything and put outside the combobox class , it is here for convenience only Public Class Dice Property Name As String = "" Property Picture As Image = Nothing Property Value As Integer = 0 Public Overrides Function ToString() As String Return Name End Function End Class ' the list of items to put in the combobox, same can be put outside the class, it is here for convenience only Public list_dice As New List(Of Dice) From { New Dice With {.Name = "D4", .Value = 4, .Picture = My.Resources.D4}, New Dice With {.Name = "D6", .Value = 6, .Picture = My.Resources.D6}, New Dice With {.Name = "D8", .Value = 8, .Picture = My.Resources.D8}, New Dice With {.Name = "D10", .Value = 10, .Picture = My.Resources.D10}, New Dice With {.Name = "D12", .Value = 12, .Picture = My.Resources.D12}, New Dice With {.Name = "D20", .Value = 20, .Picture = My.Resources.D20}, New Dice With {.Name = "D100", .Value = 100, .Picture = My.Resources.D100} } Public Sub New() Me.DropDownStyle = ComboBoxStyle.DropDownList Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable MyBase.Items.Clear() MyBase.Items.AddRange(list_dice.ToArray) End Sub Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs) 'when control is created with designer, it adds 2 times the items, this will remove them ' if the control is created via code, it works correctly and the following code is not needed If MyBase.Items.Count > 7 Then For i = MyBase.Items.Count - 1 To 7 Step -1 MyBase.Items.RemoveAt(i) Next End If Dim d As New Dice With {.Name = "", .Value = 0, .Picture = My.Resources.D100} If e.Index <> -1 Then d = list_dice(e.Index) End If If (e.State And DrawItemState.ComboBoxEdit) = DrawItemState.ComboBoxEdit Then ' Draw the contents of the edit area of the combo box. e.Graphics.DrawImage(d.Picture, New Point(2, e.Bounds.Top + 2)) e.Graphics.DrawString(If(d.Name = Nothing, "Empty", d.Name), Me.Font, Brushes.Black, 65, e.Bounds.Top + 22) Else ' Draw the contents of an item in the drop down list. e.Graphics.DrawImage(d.Picture, New Point(2, e.Bounds.Top + 2)) e.Graphics.DrawString(d.Name, Me.Font, Brushes.Black, 65, e.Bounds.Top + 22) ' Draw the focus rectangle. e.DrawFocusRectangle() End If MyBase.OnDrawItem(e) End Sub 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:
Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load Dim cb As New Pic_combobox With {.Location = New Point(40, 40), .ItemHeight = 60, .Width = 150} Me.Controls.Add(cb) End Sub 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 :
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)
-
Oct 1st, 2020, 08:26 AM
#2
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
-
Oct 1st, 2020, 09:57 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|