InvalidArgument -1 值对索引无效,在使用 Windows 窗体时 checkedboxlist

InvalidArgument -1 value not valid for index, when doing with windows forms checkedboxlist

提问人:Marcel Majhenic 提问时间:8/9/2023 最后编辑:Heretic MonkeyMarcel Majhenic 更新时间:8/9/2023 访问量:42

问:

namespace ZzCleaner
{
    public partial class Form1 : Form
    {
        //private CheckedListBox FoldersToClean;
        //private TextBox textBox2; 
        public Form1()
        {
            InitializeComponent();
        }

        //Making directories
        private void ZzCleaner_Load(object sender, EventArgs e) 
        {
            //Checking if index is really - 1, but here shows 0. 
            if (!string.IsNullOrEmpty(Properties.Settings.Default.FoldersToClear))  
            { 
                StringBuilder selectedItems = new StringBuilder();
                StringBuilder indexOf = new StringBuilder();
 
                foreach (object item in FoldersToClear.Items)
                {
                    
                    selectedItems.Append(item.ToString());
                    selectedItems.Append("," + FoldersToClear.Items.IndexOf(item));

                }
                string selectedItemsString = selectedItems.ToString();

                //And here where I actually need it, it shows -1 it pops out the MessageBox 2-times
                Properties.Settings.Default.FoldersToClear.Split(',') 
                    .ToList()
                    .ForEach(item =>
                    {   
                        try
                        {
                            var index = this.FoldersToClear.Items.IndexOf(item);
                            var checkbox = this.FoldersToClear.GetItemChecked(index);
                            FoldersToClear.SetItemChecked(index, checkbox);
                        }
                        catch (ArgumentOutOfRangeException ex)
                        {
                            MessageBox.Show(ex.Message + selectedItemsString); 
                        }
                    });
            }
        }

        private void FoldersToClean_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            
        }

        private void Form1_FormClosing(object sender, EventArgs e)
        {
            var indices = this.FoldersToClear.CheckedItems.Cast<string>().ToArray();
            /*This was meant to be for test but it doesn't show me anything. Not sure if FormClosing event even works
            //string storedFolders = string.Join(",", indices); 
            //Console.WriteLine(storedFolders); */
            Properties.Settings.Default.FoldersToClear = string.Join(",", indices);
            Properties.Settings.Default.Save();

            
        }

        private void removeBtn2_Click(object sender, EventArgs e) //Add
        {
            if (textBox2.Text == "")
            {
                MessageBox.Show(this, "Choose a folder to add. ");
                return; 
            }

            string folder = textBox2.Text;
            textBox2.Text = "";

            FoldersToClear.Items.Add(folder);
        }

        private void removeBtn1_Click(object sender, EventArgs e) //Remove
        {
            if(FoldersToClear.Items.Count != 0)
            {
                if(FoldersToClear.SelectedIndex != -1)
                {
                    int folderToDelete = FoldersToClear.SelectedIndex;
                    FoldersToClear.Items.RemoveAt(folderToDelete);
                    
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

    }
}

我正在构建一个 Windows 窗体应用程序,我遇到了一个问题,它抛出异常 ArgumentOutOfRangeException,其中索引为 -1 对索引无效,但在测试用例中,例如它高于它为 0。

此外,功能甚至不起作用,它在控制台中不起作用,甚至在.Form1_FormClosingMessageBox

现在,它只是存储数据,所以当我关闭它并重新打开它时,它包含保存的数据(复选框是否选中,并添加文件夹)。

C# WinForms CheckboxList 无效参数

评论

0赞 LarsTech 8/9/2023
您是否订阅了表格关闭活动?可以通过重写 OnFormClosing 方法来避免该问题,
1赞 ourmandave 8/9/2023
是什么引发了异常?也许在你进入闭幕活动之前有什么东西爆炸了。如果你使用的是 .NET 5,则它们会更改控件以引发 ArgOutofRange,而不是 Null Reference 等。

答: 暂无答案