locked
Error An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll when using CheckedListBox RRS feed

  • Question

  • So, I'm trying to use a CheckedListBox to list files to the list. With that, the user then picks the files he/she wants to export to the folder of their choice.

    The issue here, is that when the program tries to check which items were checked (and export the files), I get this error:

    An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll
    Additional information:  InvalidArgument=Value of '9' isn't a valid entry of 'index'. 

    Why is that? And can I specify the file that the user wants to export (So, if he picks File Six, it will copy GameSave06.isgsf)?

    Code for file extraction below:

    var eli = ExportFolderDialog.ShowDialog();
                if (eli != DialogResult.OK) return;
                try
                {
                    for (_n = 1; _n <= 24; _n++)
                    {
                        if (checkedListBox1.GetItemCheckState(_n) == CheckState.Checked)
                        {
                            MessageBox.Show((checkedListBox1.GetItemCheckState(_n) == CheckState.Checked).ToString());
                            if (_n < 10)
                                File.Copy(SaveLocation + "\\" + "GameSave0" + _n + ".isgsf",
                                    ExportFolderDialog.SelectedPath + "\\" + "GameSave0" + _n + 1 + ".isgsf", true);
                            else
                            {
                                File.Copy(SaveLocation + "\\" + "GameSave" + _n + ".isgsf",
                                    ExportFolderDialog.SelectedPath + "\\" + "GameSave" + _n + 1 + ".isgsf", true);
                            }
                        }
                    }
                    MessageBox.Show(@"Export Complete!");
                }
                catch (Exception)
                {
                    MessageBox.Show(@"Failed to export!");
                }
                Close();


    • Moved by CoolDadTx Tuesday, August 30, 2016 1:59 PM Winforms related
    Tuesday, August 30, 2016 12:26 PM

Answers

  • Hi AceOfAces_Mod,

    Based on your code, I found that the count of checkbox list is less than 24. So I suggest you change your code as following to fix your error. checkedListBox1.Items.Count will return the count of checkbox list.

    for (_n = 1; _n < checkedListBox1.Items.Count; _n++)
    {
    
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 2, 2016 7:40 AM

All replies

  • Hi.

    By default using c# collections are 0 based.

    You can try to change your for loop in this way:

      for (_n = 0; _n < 24; _n++)


    Regards,
    Bubu
    http://zsvipullo.blogspot.it

    Please mark my answer if it helped you, I would greatly appreciate it.

    Tuesday, August 30, 2016 12:51 PM
  • In .NET indexes start at 0, not 1. As such For-loops have to start at 0 and the highest Index is Lenght or count -1.

    Other problems:

    Also that exception handling could use some improovements. See here for details:
    Exception Handling Best Practices in .NET - CodeProject

    You are building paths using string conaction. Generally you should prefer Path.Combine() and similar methods.

    The code is overall quite confusing:
    A loop variable called _n instead of plain 'i'?
    You make 24 itterations, without checking if there ever are 24 items in the collection.
    There is a special case for index 10?


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Tuesday, August 30, 2016 12:55 PM
  • Let me explain a bit more, so it makes more sense:

    When the export menu opens, it lists the files in the checkedListBox like this:

    private void ExportTool_Load(object sender, EventArgs e)
            {
                
    
                for (_n = 1; _n <= 24; _n++)
                {
                    if (File.Exists(SaveLocation + "GameSave0" + _n + ".isgsf") || File.Exists(SaveLocation + "GameSave" + _n + ".isgsf"))
                        checkedListBox1.Items.Add("Slot " + _n);
                }
            }
    Yeah, it doesn't list any files that are not present on the folder so the list is smaller than that.

    The Export code's supposed to look for the file selected on the list and copy it over to the folder the user selected.
    Tuesday, August 30, 2016 1:42 PM
  • Hi AceOfAces_Mod,

    Based on your code, I found that the count of checkbox list is less than 24. So I suggest you change your code as following to fix your error. checkedListBox1.Items.Count will return the count of checkbox list.

    for (_n = 1; _n < checkedListBox1.Items.Count; _n++)
    {
    
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 2, 2016 7:40 AM