Untitled


SUBMITTED BY: Guest

DATE: June 23, 2014, 5:11 a.m.

FORMAT: Text only

SIZE: 3.2 kB

HITS: 1166

  1. I came across some code the other day that I found useful and figured I would pass it along. This code is a VB .NET custom control that adds an image to each item of a combobox control. I mad a few changes to the code to convert it into a standalone class. Hope someone out there finds this useful.
  2. 'Main Implementation
  3. Imports System.Windows.Forms
  4. Imports System.Drawing
  5. Namespace ExtCon
  6. Public Class PictureCombobox
  7. Inherits ComboBox
  8. Private imgList As ImageList
  9. Public Sub AddItem(ByVal img As Image, ByVal name As String)
  10. imgList.Images.Add(img)
  11. Me.Items.Add(name)
  12. Me.ItemHeight = imgList.ImageSize.Height
  13. Me.Width = Me.imgList.ImageSize.Width + 18
  14. End Sub
  15. Private Sub PictureCombobox_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
  16. If e.Index <> -1 Then
  17. e.Graphics.DrawImage(imgList.Images(e.Index), e.Bounds.Left, e.Bounds.Top)
  18. End If
  19. End Sub
  20. Private Sub PictureCombobox_MeasureItem(sender As Object, e As System.Windows.Forms.MeasureItemEventArgs) Handles Me.MeasureItem
  21. e.ItemHeight = imgList.ImageSize.Height
  22. e.ItemWidth = imgList.ImageSize.Width
  23. End Sub
  24. End Class
  25. End Namespace
  26. 'Designer code
  27. Partial Class PictureCombobox
  28. Inherits System.ComponentModel.Component
  29. <System.Diagnostics.DebuggerNonUserCode()> _
  30. Public Sub New(ByVal container As System.ComponentModel.IContainer)
  31. MyClass.New()
  32. 'Required for Windows.Forms Class Composition Designer support
  33. If (container IsNot Nothing) Then
  34. container.Add(Me)
  35. End If
  36. End Sub
  37. <System.Diagnostics.DebuggerNonUserCode()> _
  38. Public Sub New()
  39. MyBase.New()
  40. 'This call is required by the Component Designer.
  41. InitializeComponent()
  42. End Sub
  43. 'Component overrides dispose to clean up the component list.
  44. <System.Diagnostics.DebuggerNonUserCode()> _
  45. Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  46. Try
  47. If disposing AndAlso components IsNot Nothing Then
  48. components.Dispose()
  49. End If
  50. Finally
  51. MyBase.Dispose(disposing)
  52. End Try
  53. End Sub
  54. 'Required by the Component Designer
  55. Private components As System.ComponentModel.IContainer
  56. 'NOTE: The following procedure is required by the Component Designer
  57. 'It can be modified using the Component Designer.
  58. 'Do not modify it using the code editor.
  59. <System.Diagnostics.DebuggerStepThrough()> _
  60. Private Sub InitializeComponent()
  61. components = New System.ComponentModel.Container()
  62. End Sub
  63. End Class
  64. Feel free to donate if you find this useful.
  65. BTC: 19fnZ8D3UAcr1hDkVc4USbxHrFtVNuDUP5
  66. LTC: LiC4tbyTvjzHDN9K1zcJ4RdP53QbtDRgGp
  67. DOGE: D6jxxMURuLXsupesw9YmSzSwMQwQxGM9aG

comments powered by Disqus