Extending a ComboBox flexibility with the ComboBox.ObjectCollection - ArgumentOutOfRangeException

  • Thread starter Desmond Cassidy
  • Start date
D

Desmond Cassidy

Hi,
I have been trying to figure out what is going on for some time on this one.

I have added a UserControl and done the usual changes
a) Changed the UserControl1.vb to XXX_Combo.vb
b) Changed the Inheritence in the XXX_Combo.Designer.vb to Inherits System.Windows.Forms.ComboBox
c) Perform an immediate Build thus the Control (XXX_Combo) is visible in the Form Toolbox available to add to the form.

Now I add the XXX_Combo (from the Toolbox) to the Form and start entering the code in the Class XXX_Combo

a) Add the Imports statement : Imports System.Windows.Forms
b) Add a Constructor
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.DrawMode = Windows.Forms.DrawMode.Normal
Me.DropDownStyle = ComboBoxStyle.DropDown
Me.DropDownHeight = 100
End Sub

c) Add a Nested Class ComboBoxItem which has
i) 3 Constructors - New() ; New(String) ; New(String,Integer) - which allows me to add an index based Image to the ComboBox
ii) 2 Properties - Text; ImageIndex
iii) Overridden Function ToString (to return the Value of the Item

Now for the sake of clarity (and using the OnDrawItem) and getting to the root of the problem I do not use the actual Image facility - basically I have backtracked to the essential elements of the class.

So, in the Form I added some simple code.

Me.XXX_Combo1.Items.Add("XXX_Detail 0")
Me.XXX_Combo1.Items.Add("XXX_Detail 1")
Me.XXX_Combo1.Items.Add(New XXX_Combo.ComboBoxItem("XXX_Detail 2"))
Me.XXX_Combo1.Items.Add(New XXX_Combo.ComboBoxItem("XXX_Detail 3"))
Me.XXX_Combo1.Items.Add(New XXX_Combo.ComboBoxItem("XXX_Detail 4", 0))

This code should use the default ComboBox.ObjectCollection for the Add Method though using the 2nd Constructor for Items 2 and 3 - and the 3rd Constructor for Item 4

Run and everything works including the Selection on all items (i.e. displayed in the top of the XXX_ComboBox)

====================================================================================

So, Rather that have a mucky Add Method I would like to have an Add Method for Items 4 that would look similar to

Me.XXX_Combo1.Items.Add("XXX_Detail 4", 0)

Now form what I have scoured on the forums etc the best way is to create a Class which inherits the ComboBox.ObjectCollection and Overload the Add Function.

So, I create a Class XXX_ComboCollection which Inherits the ComboBox.ObjectCollection ( not sure whether this should be the System.ComboBox.ObjectCollection or the XXX_Combo.ObjectCollection ?)
a) Add the Constructor (which Intellisense tells you to do!
Public Sub New(ByVal Owner As ComboBox)
MyBase.New(Owner)
End Sub

b) DO NOT YET Add the Overloaded Add Function with String and Integer.

Create the hooks from/in the XXX_ComboClass
Public Shadows Items As XXX_ComboCollection = New XXX_ComboCollection(Me)

Run the Code, as before ALL the Items get displayed correctly in the XXX_ComboBox BUT as soon as I Select an items then the Exception appears and goes all the way up to the Form designer code
Partial Class Form1
Inherits System.Windows.Forms.Form

The Exception varies only based on which item I choose (index based as you can see from the message)
InvalidArgument=Value of '2' is not valid for 'index'.
Parameter name: index

=================================================================================

It seems that I have declared the ObjectCollection class incorrectly or insufficiently ? or I have created a 2nd instance which is causing the problem. I have searched high and low for a working example of this to no avail...

Anyone out there got any ideas ???


Cheers,

Desmond.
 
D

Desmond Cassidy

I did some more digging and found a couple of interesting points.

a) After Adding the Items to the ComboBox I set the SelectedIndex to a valid number (i.e. less than the number of Items added) and it crashed.
b) In debug I found that the ComboBox.Items had all the items added but the 'itemsCollection' was set to Nothing - I reverted to earlier code where the selection was OK and found that the 'itemsCollection' contained the valid collection.

I guess what I am saying is that there is some 'glue' missing between the Items and the 'itemsCollection' - goodness knows where ???

Anyone ?

Cheers,

Desmond...
 

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