Solved

Inheriting from a vb.net combobox

Posted on 2012-03-13
9
357 Views
Last Modified: 2012-03-14
Hi

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
0
Comment
Question by:IssacJones
  • 4
  • 4
9 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 37714389
see below
Public Class MyCombo
    Inherits ComboBox
    Protected Overrides Sub SetItemCore(index As Integer, value As Object)
        MyBase.SetItemCore(index, value)

    End Sub
End Class

Open in new window

0
 

Author Comment

by:IssacJones
ID: 37714547
I've tried your suggestion but it doesn't seem to be called i.e. I placed a breakpoint there and it wasn't called.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37714585
did you use the new combobox? e.g
Dim xCombo As New MyCombo
Form1.Controls.Add(xCombo)

Open in new window

or add the class and compile the project, then the MyCombo will appear at the top of the Toolbox.
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:IssacJones
ID: 37714687
Hi

Yes, I have added the basic MyComboBox e.g.

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
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37717764
What do you want to achieve by overriding?
0
 

Author Comment

by:IssacJones
ID: 37718924
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.

Can anybody tell me how to achieve this?

John
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 300 total points
ID: 37719098
There are a few ways to skin a cat! Rather than override, why not implement your own Add method?
Public Class ExtendedComboBox
    Inherits System.Windows.Forms.ComboBox
    Sub MyAdd(ByVal Value As String)
        Me.Items.Add(Value.Trim)
    End Sub
End Class

Open in new window

0
 

Author Comment

by:IssacJones
ID: 37719296
poor cat :)

Yes, good idea!

I'm still curious how to override the Add which is built in though.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37719407
I actually believe thats the only way to "override" it. Seems simple when you are looking for a complex solution, but that is its beauty!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question