By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,606 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

Combobox fun - "Specified argument was out of the range..."

P: n/a
Hi... Synposis...

Throws exception:
"Specified argument was out of the range of valid values."
Read on for the juicy tidbits.

MySimpleClassCol mscc=new MySimpleClassCol();

private void InitCombo()
{
// Doing this so bind op doesn't fire event
m_combo.SelectedIndexChanged -= new System.EventHandler...
mscc.Clear();
XmlNodeList nl=m_xml.DocumentElement.SelectNodes("/a/b/c/d");
foreach(XmlNode n in nl)
{
// Grab a bunch of strings from the node innerxml
MySimpleClass sc=new MySimpleClass(s1, s2, s3, s4);
mscc.Add(sc);
}
m_combo.DataSource=null;
m_combo.DataSource=mscc;
m_combo.DisplayMember="ComboName";
m_combo.SelectedIndex=-1;
m_combo.SelectedIndex=-1;
m-combo.SelectedIndexChanged += new System.EventHandler...
try
{
if(m_combo.Items.Count>0)
m_combo.SelectedIndex=0;
}
catch(Exception ex)
{
Debug.WriteLine(ex.Message);
}
}

InitCombo is called on Form load. It is also called whenever a change
is made to the underlying XML document.

Scenario 1:
1. Add a <d> node to the XML document
2. Save XML document to disk
3. InitCombo()
4. m_combo.SelectedIndex=m_combo.FindStringExact(newV alue);

This works.

Scenario 2:
1. Remove an inner <d> XML node - NOT the last one added
2. Save XML document to disk
3. InitCombo()
4. Leave m_combo.SelectedIndex=0;

This works.

Scenario 3:
1. Remove the last XML node added - always last child of parent node <c>
2. Save XML document to disk
3. InitCombo()

THIS BLOWS UP on:

try
{
if(m_combo.Items.Count>0) // which it is...
m_combo.SelectedIndex=0; // Throws exception
}

The exception thrown is:
"Specified argument was out of the range of valid values.
Parameter name: Index was out of range.
Must be non-negative and less than the size of the collection."

Nice... except that .Count is non-zero, 0 is non-negative, and less than
the size of the collection. And it ONLY happens when I remove the last child
in the XML... which corresponds to the last item in the list.

<a>
<b>
<c>
<d name="OKToDelete">
</d>
<d name="OKToDelete">
</d>
<d name="OKToDelete">
</d>
<d name="BLOWS_UP_ONDelete">
</d>
</c>
</b>
</a>

I have fiddled with the event handlers. I've tried NOT rebinding and just
repopulating the data set. I have all but waved a dead chicken over
the keyboard.

Could anyone throw me a bone here?

Thanks,
M.

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Michael,

The combobox fun is mostly when you change an index in an indexchange event.
That fires it again in the middle. The in my opinion most simple solution
than. Start event with removing the Event.Handler and add it again leaving
that event..

(I don't see that event)

I hope this helps something?

Cor
Nov 16 '05 #2

P: n/a
Cor,

Thanks for the response. I tried disabling the event handler, but it made
very little difference. Now instead of the form disappearing due to the
exception, the combobox refills with Class.Property. The text box of the
control displays Class.Property, but the actual dropdown list shows the
correct items. Clicking on any one of them yields the exception:

ArgumentOutOfRange

on every line past ***

m_combo.DataSource=null;
m_combo.DataSource=m_bcs;
***
m_combo.DisplayMember="ComboName";
m_combo.SelectedIndex=-1;

On break, m_bcs shows .Count=4. ToString() clearly shows "ComboName".

What's more puzzling than anything is why this works on everything except
the removal of the last element in m_bcs. Remove or add anything anywhere
else and there are NO issues. I have a suspicion it's more to do with the
databinding process than fiddling with the indices, although I did try
disabling/reenabling the event handler in the combobox, and in other items
that are influenced by the combo box... no help. Go fish?

Cheers,
M.

"Cor Ligthert" wrote:
Michael,

The combobox fun is mostly when you change an index in an indexchange event.
That fires it again in the middle. The in my opinion most simple solution
than. Start event with removing the Event.Handler and add it again leaving
that event..

(I don't see that event)

I hope this helps something?

Cor

Nov 16 '05 #3

P: n/a
Michael,

Disabling the last item in a collection does not give only with a combobox
often problems.
That is the reason why in loops this is often done from the last to the
first.

Cor
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.