Search code examples
c#winformscomboboxcustom-controlstype-bounds

type-bound custom ComboBox deriving from ComboBox


I am supposed to create a custom ComboBox by deriving a class from ComboBox in my WinForms application. I have never done this before and not able to find many good example from Google.

I am required to derive a custom combobox so that I can make the custom combobox type-bound to a particular object.

This is what I have so far.

CustomComboBox.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MAPClient {
    class MAPCodeComboBox : ComboBox {

    }
}

I have some specific questions:

  1. Which methods do I need to override?
  2. How do I use it in my VS2010 designer mode?

Solution

  • Okay, finally I have the following for a custom type-bounded ComboBox. Let me know if I am doing something wrong.

    MAPComboBox.cs

    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace MAPClient {
        class MAPComboBox : ComboBox {
            private MAPCodeObjectCollection MAPCodeItemCollection = null;
    
            new public MAPCodeObjectCollection Items {
                // override
            }
    
            new public List<MAPCode> DataSource {
                // override
            }
    
            public MAPCodeComboBox() { }
        }
    }
    

    MAPCodeObjectCollection.cs

    using System.Windows.Forms;
    
    namespace MAPClient {
        class MAPCodeObjectCollection : ComboBox.ObjectCollection {
            public MAPCodeObjectCollection(ComboBox owner) : base(owner) { }
    
            new public int Add(object item) {
                // override
            }
    
            new public void Insert(int index, object item) {
                // override
            }
        }
    }