Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. <DesignerCategory("UserControl")>
  2. Public Class MyComboBox : Inherits ComboBox
  3.  
  4. <Editor(GetType(CollectionEditor), GetType(UITypeEditor))>
  5. <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
  6. <Category("(Extended) Appearance")>
  7. Public Shadows ReadOnly Property Items As MyItemCollection
  8. Get
  9. Return Me.itemsB
  10. End Get
  11. End Property
  12. Private ReadOnly itemsB As MyItemCollection
  13.  
  14. <DebuggerStepThrough>
  15. Public Sub New()
  16. MyBase.DrawMode = DrawMode.OwnerDrawFixed
  17. Me.itemsB = New MyItemCollection(Me)
  18. End Sub
  19.  
  20. Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
  21.  
  22. e.DrawBackground()
  23. e.DrawFocusRectangle()
  24.  
  25. ' Check if it is an item from the Items collection.
  26. If (e.Index < 0) Then
  27. ' not an item, draw the text.
  28. Using brush As New SolidBrush(e.ForeColor)
  29. e.Graphics.DrawString(Me.Text, e.Font, brush, e.Bounds.Left, e.Bounds.Top)
  30. End Using
  31.  
  32. Else
  33. ' Get the item to draw.
  34. Dim item As MyItem = Me.Items(e.Index)
  35. Using brush As New SolidBrush(e.ForeColor)
  36. e.Graphics.DrawString(item.Text, e.Font, brush, e.Bounds.Left, e.Bounds.Top)
  37. End Using
  38.  
  39. End If
  40.  
  41. MyBase.OnDrawItem(e)
  42.  
  43. End Sub
  44.  
  45. End Class
  46.  
  47. <DesignerCategory("Code")>
  48. <DefaultMember("Item")>
  49. <ListBindable(False)>
  50. Public Class MyItemCollection : Inherits ObjectCollection
  51.  
  52. Sub New(ByVal owner As MyComboBox)
  53. MyBase.New(owner)
  54. End Sub
  55.  
  56. <Browsable(False)>
  57. <DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
  58. Default Public Overloads Property Item(ByVal index As Integer) As MyItem
  59. Get
  60. Return DirectCast(MyBase.Item(index), MyItem)
  61. End Get
  62. Set(ByVal value As MyItem)
  63. MyBase.Item(index) = value
  64. End Set
  65. End Property
  66.  
  67. ' Original property:
  68. '
  69. '<Browsable(False)>
  70. '<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
  71. 'Default Public Overrides Property Item(ByVal index As Integer) As Object
  72. ' Get
  73. ' Return MyBase.Item(index)
  74. ' End Get
  75. ' Set(value As Object)
  76. ' MyBase.Item(index) = value
  77. ' End Set
  78. 'End Property
  79.  
  80. End Class
  81.  
  82. <DesignerCategory("Code")>
  83. <Browsable(False)>
  84. <ToolboxItem(False)>
  85. Public NotInheritable Class MyItem
  86.  
  87. Public Property Text As String
  88.  
  89. Public Sub New()
  90. Me.New(String.Empty)
  91. End Sub
  92.  
  93. Public Sub New(ByVal text As String)
  94. Me.Text = text
  95. End Sub
  96.  
  97. End Class
  98.  
  99. Private Sub InitializeComponent()
  100. Me.MyComboBox1 = New WindowsApplication52.MyComboBox()
  101. Dim MyItem1 As WindowsApplication52.MyItem = New WindowsApplication52.MyItem()
  102. MyItem1.Text = "Test"
  103. Me.MyComboBox1.Items.AddRange(New Object() {MyItem1})
  104.  
  105. ' ...
  106. End Sub
  107.  
  108. Public Class Form1 : Inherits Form
  109.  
  110. Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
  111.  
  112. MessageBox.Show(String.Format("There are '{0}' items in the collection.", MyComboBox1.Items.Count))
  113.  
  114. End Sub
  115.  
  116. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement