Solved

ListBox Error

Posted on 2004-04-05
11
393 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:jvoros1
  • 6
  • 3
  • 2
11 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 10762354
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
 
LVL 9

Expert Comment

by:tinchos
ID: 10762361
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
 

Author Comment

by:jvoros1
ID: 10762371
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
 

Author Comment

by:jvoros1
ID: 10762379
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
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10763513
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:jvoros1
ID: 10766868
I tried what you recomended and i am still getting the same error :(
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 10766922
Can you debug and say on which line does the error occur?
0
 

Author Comment

by:jvoros1
ID: 10767044
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
 

Author Comment

by:jvoros1
ID: 10767075
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
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
ID: 10767186
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
 

Author Comment

by:jvoros1
ID: 10767213
Wow that did it Thank you sooooooooooooo much i have been stuck on this forever
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now