Overriding / customizing ComboBox

G

Gianluca Zangara

Hi, I would override a Windows.Forms.Combobox to have a
MyObjectCollection instead of a ObjectCollection as Items property.
I've try writing this code, but it seems that items are stored somewhere
else (not in Items property). I can add items to collection but when I
try to select an item from the combobox (at runtime) an exception tells
me that there is no such element in Items (litterally it tells me that
"value 0 for index not valid")
what local variable i have to override to make this code fully working?

Public Class FullComboBox
Inherits ComboBox
Private _Items As FullObjectCollection

Public ReadOnly Property Items() As FullObjectCollection
Get
If _Items Is Nothing Then
_Items = New FullObjectCollection(Me)
End If
Return _Items
' Return _Items
End Get
End Property
End Class

Public Class FullObjectCollection
Inherits ComboBox.ObjectCollection

Public Sub New(ByVal owner As ComboBox)
MyBase.New(owner)
End Sub

Public Overloads Sub Add(ByVal ID As String, ByVal val As String)
MyBase.Add(New objectItem(ID, val))
End Sub

Public Overloads Sub Add(ByVal val As Object)
MyBase.Add(New objectItem("", val))
End Sub

Default Public Overloads Property Item(ByVal index As Integer) As
objectItem
Get
Return MyBase.Item(index)
End Get
Set(ByVal value As objectItem)
MyBase.Item(index) = value
End Set
End Property
End Class

Public Class objectItem
Public ID As String
Public val As String

Public Sub New(ByVal _ID As String, ByVal _val As String)
ID = _ID
val = _val
End Sub

Public Overrides Function ToString() As String
Return val
End Function
End Class



thanks in advance
Gianluca Zangara
 
F

Family Tree Mike

Hi, I would override a Windows.Forms.Combobox to have a
MyObjectCollection instead of a ObjectCollection as Items property.
I've try writing this code, but it seems that items are stored somewhere
else (not in Items property). I can add items to collection but when I
try to select an item from the combobox (at runtime) an exception tells
me that there is no such element in Items (litterally it tells me that
"value 0 for index not valid")
what local variable i have to override to make this code fully working?

Public Class FullComboBox
Inherits ComboBox
Private _Items As FullObjectCollection

Public ReadOnly Property Items() As FullObjectCollection
Get
If _Items Is Nothing Then
_Items = New FullObjectCollection(Me)
End If
Return _Items
' Return _Items
End Get
End Property
End Class

Public Class FullObjectCollection
Inherits ComboBox.ObjectCollection

Public Sub New(ByVal owner As ComboBox)
MyBase.New(owner)
End Sub

Public Overloads Sub Add(ByVal ID As String, ByVal val As String)
MyBase.Add(New objectItem(ID, val))
End Sub

Public Overloads Sub Add(ByVal val As Object)
MyBase.Add(New objectItem("", val))
End Sub

Default Public Overloads Property Item(ByVal index As Integer) As
objectItem
Get
Return MyBase.Item(index)
End Get
Set(ByVal value As objectItem)
MyBase.Item(index) = value
End Set
End Property
End Class

Public Class objectItem
Public ID As String
Public val As String

Public Sub New(ByVal _ID As String, ByVal _val As String)
ID = _ID
val = _val
End Sub

Public Overrides Function ToString() As String
Return val
End Function
End Class



thanks in advance
Gianluca Zangara

Why aren't you showing a Set method for the Items property of FullComboBox?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top