Combobox exception - xpost/C# forum

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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(newValue);

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.
 
Michael,

What you do is not crosposting however multiposting. Crossposting is sending
one message to more relevant newsgroups in one time. Multiposted is hated,
crossposting is found by a lot of people as me very fine. (That prevent
messages as this)

I have made an answer in the C# newsgroup.

Cor
 
Back
Top