0

I'm trying to create a new control that inherits the ComboBox. I've added a new method that does what I need, but I'd like to access it through ComboBox.Items while keeping everything else intact. Shadowing seems to wipe out the entire Items subclass. I can live with it the way it is if this isn't possible, but it seems more logical to me to have the method in the Items subclass.

Is there any way to do this?

Public Class AdvComboBox
    Inherits System.Windows.Forms.ComboBox

    Public Class ComboItem
        Dim Text As String
        Dim ID As Long

        Public Sub New(ItemText As String, ItemID As Long)
            Text = ItemText
            ID = ItemID
        End Sub

        Public ReadOnly Property ItemText
            Get
                Return Text
            End Get
        End Property

        Public ReadOnly Property ItemID
            Get
                Return ID
            End Get
        End Property
    End Class

    'This is the method I'd like to access from ComboBox.Items
    Public Sub AddAdvItem(DisplayText As String, ID As Long)
        DisplayMember = "ItemText"
        ValueMember = "ItemID"
        Items.Add(New ComboItem(DisplayText, ID))
    End Sub
End Class
  • You can't not the way you want I am afraid... – Trevor Jan 30 '16 at 18:01
  • Also you're reinventing the wheel. Look at `ComboxItem` which has your properties you need. There's other ways but this should help with some direction. – Trevor Jan 30 '16 at 18:12
  • It's likely that this will get expanded on and more properties added so it looks like I'll just have to live with it the way it is. Thanks anyway. – Keith Krause Jan 30 '16 at 18:37
  • I think `ComboBox` already have functionality you trying to achieve. If you show example how you want use your control, you will get answer to question does you ever need that – Fabio Jan 30 '16 at 18:46
  • 1
    see http://stackoverflow.com/q/24762478/1070452 Most of what you want to do can be done by binding the existing CBO to a `List(Of ComboItem)` – Ňɏssa Pøngjǣrdenlarp Jan 30 '16 at 19:35

1 Answers1

0

It is possible to replace the Items property with your own implementation by shadowing it. E.g. Public Shadows ReadOnly Property Items As CustomObjectCollection

You can then create your own CustomObjectCollection class that references the built in Items collection and exposes all of the required methods plus your new one.

However, having said all that I would always use an external list/dictionary/datatable/BindingList for the data and just bind it to the Combobox rather than using the Items collection.

John0987
  • 63
  • 5