I want to create a class from a ComboBox and modify the Add property so that when I do something like:
Me.cbMyCombo.Items.Add(objComboItem)
I can modify the data stored in the objComboItem variable within the new class.
Can anybody tell me how to do this?
John
Visual Basic.NET
Last Comment
nepaluz
8/22/2022 - Mon
nepaluz
see below
Public Class MyCombo Inherits ComboBox Protected Overrides Sub SetItemCore(index As Integer, value As Object) MyBase.SetItemCore(index, value) End SubEnd Class
Public Class MyComboBox
Inherits System.Windows.Forms.ComboBox
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Protected Overrides Sub SetItemCore(ByVal index As Integer, ByVal value As Object)
MyBase.SetItemCore(index, value)
End Sub
End Class
This has been added to my solution and I have created a combo of this type.
What I want to do is when doing this (where cbMyCombo is of type MyComboBox):
Me.cbMyCombo.Items.Add(objComboItem)
is to override the Add so I can modify the objComboItem within the new class.
John
Nasir Razzaq
What do you want to achieve by overriding?
IssacJones
ASKER
Hi
I have made a little more progress with the following:
Public Class ExtendedComboBox
Inherits System.Windows.Forms.ComboBox
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
End Sub
Public Overloads ReadOnly Property Items As ComboBox.ObjectCollection
Get
Return MyBase.Items
End Get
End Property
End Class
This allows me to deal with getting the Items property. What I now need to do is deal with the Add which is called when populating the combobox e.g.
Me.cbMyCombo.Items.Add(objComboItem)
What I'm trying to do is when objComboItem is passed into the combo it may contain text. I want to remove all whitespace from the text passed in within my inherited combobox. That is, if I pass in " a test ", then it would be stored and displayed as "a test".
I have been able to do this externally by stripping the white space before passing it into the combo box BUT it seems more logical to me that it is done within the combo i.e. in the process of passing it into the combo box so it is done automatically. Besides, I'm intrigued on how it can be done anyway.
I looks as if the Add memeber I'm trying to override does not exist in the combobox class but rather is in a parent of the combobox i.e.
ComboBox.ObjectCollection.Add Method
If I can override this then I can strip the whitespace during the "Add" process.
Open in new window