You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WinForms CheckedListBox索引越界及FormClosing事件失效问题

问题解决方案

问题1:ArgumentOutOfRangeException异常修复

异常根源

你在ZzCleaner_Load里犯了两个核心错误:

  1. 程序启动时FoldersToClear.Items是空的,你直接拿存储的文件夹路径去查索引,肯定找不到,返回-1触发异常。
  2. 当前存储逻辑只存了选中的文件夹路径,没保存所有添加的文件夹,也没记录每个文件夹的选中状态,重启后列表是空的,根本没法恢复状态。

修复思路

修改存储格式,同时保存所有文件夹路径和对应的选中状态。比如用|分隔单条记录的路径与状态,用;分隔不同记录(避免路径里的逗号干扰),示例格式:C:\Test|True;D:\Temp|False

问题2:Form1_FormClosing事件不生效修复

失效原因

WinForms事件必须手动绑定——你只写了事件方法,但没把它关联到Form的Closing事件上,所以触发不了。

修复思路

在Form构造函数里添加事件绑定代码即可。


完整修正代码

namespace ZzCleaner
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // 手动绑定FormClosing事件
            this.FormClosing += Form1_FormClosing;
        }

        private void ZzCleaner_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Properties.Settings.Default.FoldersToClear))
            {
                // 拆分存储的所有文件夹记录
                var folderEntries = Properties.Settings.Default.FoldersToClear.Split(';');
                foreach (var entry in folderEntries)
                {
                    if (string.IsNullOrEmpty(entry)) continue;
                    // 拆分路径与选中状态
                    var parts = entry.Split('|');
                    if (parts.Length != 2) continue;

                    string folderPath = parts[0];
                    bool isChecked = bool.Parse(parts[1]);

                    // 添加文件夹到列表并设置选中状态
                    int index = FoldersToClear.Items.Add(folderPath);
                    FoldersToClear.SetItemChecked(index, isChecked);
                }
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            List<string> folderEntries = new List<string>();
            // 遍历所有文件夹,记录路径和选中状态
            for (int i = 0; i < FoldersToClear.Items.Count; i++)
            {
                string folderPath = FoldersToClear.Items[i].ToString();
                bool isChecked = FoldersToClear.GetItemChecked(i);
                folderEntries.Add($"{folderPath}|{isChecked}");
            }
            // 拼接成存储字符串并保存
            Properties.Settings.Default.FoldersToClear = string.Join(";", folderEntries);
            Properties.Settings.Default.Save();
            // 弹窗验证事件触发,测试后可删除
            MessageBox.Show("数据已保存");
        }

        private void removeBtn2_Click(object sender, EventArgs e) // 添加文件夹
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show(this, "请输入要添加的文件夹路径。");
                return;
            }

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

            // 添加后默认不选中,可按需修改
            int index = FoldersToClear.Items.Add(folder);
            FoldersToClear.SetItemChecked(index, false);
        }

        private void removeBtn1_Click(object sender, EventArgs e) // 删除文件夹
        {
            if (FoldersToClear.SelectedIndex != -1)
            {
                FoldersToClear.Items.RemoveAt(FoldersToClear.SelectedIndex);
            }
        }

        // 以下空方法可保留或按需删除
        private void FoldersToClean_SelectedIndexChanged_1(object sender, EventArgs e)
        {
        }

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

额外注意事项

  • 确认Properties.Settings.Default.FoldersToClear的类型是string,若不是,去项目属性的「设置」面板修改。
  • 测试时要完全退出程序,避免进程残留导致设置未保存。
  • 如果文件夹路径可能包含|或;,可换用更特殊的分隔符(比如☃),或者用JSON序列化存储,可靠性更高。

内容的提问来源于stack exchange,提问作者Marcel Majhenic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 18:45:03