ListBox Error

Hello Expert, I am getting the following error in my listbox when i remove items from list box b to list box A. And if there are still items in the box B but none have been selected or have focus and then once i select one it gives me this error Do you know why or how i can catch it

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
jvoros1Asked:
Who is Participating?
 
TheAvengerConnect With a Mentor Commented:
OK, try this code:

ArrayList temp = new ArrayList();
foreach(ContactInfo.ContactInfoPD aContactInfo  in lstConsultantsB.SelectedItems)
  temp.Add (aContactInfo);

foreach (int index in lstConsultantsB.SelectedIndices)
  lstConsultantsB.SetSelected (index, false);

foreach(ContactInfo.ContactInfoPD aContactInfo  in temp)
{
  ArrayListBoxB.Remove(aContactInfo);              
  ArrayConsultant.Add(aContactInfo);              
}
0
 
tinchosCommented:
Hi jvoros1,

In order to catch that error you can do

try
{
     // Your code throwing Exception
}
catch( System.ArgumentOutOfRangeException e )
{
    // Code to handle exception
}

Hope this helps

Tincho
0
 
tinchosCommented:
jvoros1,

In order to know why you're getting that error, then you can check in the MSDN if the functions you're using throws that exception.

Generally you get that exception when you try to remove item i from a collection where i item is not available.

Hope this helps

Tincho
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
jvoros1Author Commented:
Here is an example of my code when i try to use the catch ArgumentOutOfRangeException  it does not help
                                private void cmdRemoveConsultant_Click(object sender, System.EventArgs e)
            {
                  try
                  {
                        if(lstConsultantsB.SelectedItems.Count != 0)
                        {
                              foreach(ContactInfo.ContactInfoPD aContactInfo  in lstConsultantsB.SelectedItems)
                              {
                                    ArrayListBoxB.Remove(aContactInfo);                  
                                    ArrayConsultant.Add(aContactInfo);                  
                              }
                              
                              lstConsultantA.DataSource = null;
                              lstConsultantA.Sorted = true;
                              lstConsultantA.DataSource = ArrayConsultant;
                              lstConsultantA.DisplayMember = "contactFullName";
                              lstConsultantA.ValueMember = "personID";
                                                                        
                              lstConsultantsB.DataSource = null;
                              lstConsultantsB.Sorted = true;
                              lstConsultantsB.DataSource = ArrayListBoxB;
                              lstConsultantsB.DisplayMember = "contactFullName";
                              lstConsultantsB.ValueMember = "personID";
                  
                              lstConsultantsB.EndUpdate();
            

                        }
                  }
                  catch( System.ArgumentOutOfRangeException ea )
                  {
                        MessageBox.Show(ea.Message);
                  }
                  catch(Exception ex)
                  {
                        MessageBox.Show(ex.Message);
                  }
            
      }
0
 
jvoros1Author Commented:
I have a list box with items as follows
Bob
John
Jane
Carrie
if i remove Carrie then there is no focus to any item and once i select one like Bob i get that error message before i can try to remove it
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index was out of range.  Must be non-negative and less than the size of the collection.
0
 
TheAvengerCommented:
The problem is in this line:

foreach(ContactInfo.ContactInfoPD aContactInfo  in lstConsultantsB.SelectedItems)

As you later remove the object from the list box, you also remove it from the selected items. Try this wolution:

ArrayList temp = new ArrayList();
foreach(ContactInfo.ContactInfoPD aContactInfo  in lstConsultantsB.SelectedItems)
  temp.Add (aContactInfo);

foreach(ContactInfo.ContactInfoPD aContactInfo  in temp)
{
  ArrayListBoxB.Remove(aContactInfo);              
  ArrayConsultant.Add(aContactInfo);              
}
0
 
jvoros1Author Commented:
I tried what you recomended and i am still getting the same error :(
0
 
TheAvengerCommented:
Can you debug and say on which line does the error occur?
0
 
jvoros1Author Commented:
I tried to do that but the error does not happen in my cmdRemove_Click which is the code above And I have no events for the list box. What happens is i get  throwen out to the main mentod as shown below and my program just closes.
                                Application.Run(new frmMainMenu());            
I have no idea how to go about to find where it happens.
I will explain again how this is happening I have two list boxes and the user from listboxA moves the consultants that are on the project to ListBoxB. When the move from A to B the consultant is removed from ListBoxA and Added to ListBoxB. But when they want to take that consultant off the project lets say these are the people
Joe
Frank
Mary
Jen
If i remove from the beginning  like Joe everything is ok it adds to list box B, and if i remove Mary or Frank it is ok but if i remove the last person in the array like Jen in crashes with that error and even when i try to catch the error it catches it takes me back to the form and when i click back on listboxB it crahes as if i had a event in there but i dont' any idea's
0
 
jvoros1Author Commented:
Ok actually if i remove the last person it removes them catches the error ArgumentOutOfRangeException' and puts focus back on the form but then if i click in the listBoxB again it crashes and sends me to Application.Run(new frmMainMenu());    
0
 
jvoros1Author Commented:
Wow that did it Thank you sooooooooooooo much i have been stuck on this forever
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.