击选项卡时,对象列表视图上的ArgumentOutOfRangeException

时间:2018-07-20 08:10:50

标签: c# winforms objectlistview

我有一个包含两个文本列的对象列表视图。当我编辑左列并单击制表符以转到右侧时,我收到一个索引为-1的“ ArgumentOutOfRangeException”。索引似乎是列表视图的内部内容,因为我调试了应用程序,未发现任何错误。这是代码:

public partial class SummaryOverviewSettingsDlg : Form
{
    private List<SummaryDataset> _localSummaryDatasets = new List<SummaryDataset>();
    private bool _includeLimits;
    private SummaryOverviewSettings _summaryOverviewSettings;

    public bool IncludeLimits { get { return _includeLimits; } }

    public SummaryOverviewSettingsDlg(SummaryOverviewSettings summaryOverviewSettings)
    {
        InitializeComponent();

        if (summaryOverviewSettings.Datasets != null)
        {
            _localSummaryDatasets.AddRange(summaryOverviewSettings.Datasets);
        }

        _summaryOverviewSettings = summaryOverviewSettings;
    }

    private void DataFilesListDlg_Load(object sender, EventArgs e)
    {
        foreach(var dataFile in _localSummaryDatasets)
        {
            olvFilePaths.AddObject(dataFile);
        }

        LimitsCheckbox.Checked = _summaryOverviewSettings.ShowLimits;
    }        

    private void OlvFilePaths_CellRightClick(object sender, CellRightClickEventArgs e)
    {
        var contextMenuSymbol = new ContextMenuStrip();
        ToolStripItem item;

        item = contextMenuSymbol.Items.Add("Add sample");
        item.Click += ContextMenuAddFilePath;

        if (e.Model != null)
        {
            contextMenuSymbol.Items.Add("-");

            item = contextMenuSymbol.Items.Add("Delete sample");
            item.Click += ContextMenuDeleteFilePath;
        }

        olvFilePaths.ContextMenuStrip = contextMenuSymbol;
    }

    private void ContextMenuAddFilePath(object sender, EventArgs e)
    {
        var item = new SummaryDataset()
        {
            SampleName = "Sample",
            Path = "Path"
        };

        _localSummaryDatasets.Add(item);

        // Rebuild the list in the GUI
        olvFilePaths.ClearObjects();

        foreach (var dataFile in _localSummaryDatasets)
        {
            olvFilePaths.AddObject(dataFile);
        }
        olvFilePaths.AutoResizeColumns();
    }
    private void ContextMenuDeleteFilePath(object sender, EventArgs e)
    {
        if (olvFilePaths.SelectedObject != null)
        {
            var item = (SummaryDataset)olvFilePaths.SelectedObject;
            olvFilePaths.RemoveObject(item);
            _localSummaryDatasets.Remove(item);
        }
    }

    private void OlvFilePaths_CellEditFinished(object sender, CellEditEventArgs e)
    {
        if (e.Control is TextBox textBox)
        {
            var oldValue = (string)e.Value;
            var newValue = (string)e.NewValue;
            var col = e.Column.AspectName;
            var index = e.ListViewItem.Index;

            if (newValue != oldValue)
            {
                if (col == "SampleName")
                {
                    _localSummaryDatasets[index].SampleName = newValue;
                }
                else
                {
                    _localSummaryDatasets[index].Path = newValue;
                }
            }
        }

        // Rebuild the list in the GUI
        olvFilePaths.ClearObjects();

        foreach (var dataFile in _localSummaryDatasets)
        {
            olvFilePaths.AddObject(dataFile);
        }
        olvFilePaths.AutoResizeColumns();
    }

    private void OkButton_Click(object sender, EventArgs e)
    {
        _summaryOverviewSettings.Datasets.Clear();
        _summaryOverviewSettings.Datasets.AddRange(_localSummaryDatasets);
        _summaryOverviewSettings.ShowLimits = _includeLimits;
        DialogResult = DialogResult.OK;
        Close();
    }

    private void ButtonCancel_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.Cancel;
        Close();
    }

    private void LimitsCheckbox_CheckedChanged(object sender, EventArgs e)
    {
        _includeLimits = LimitsCheckbox.Checked;
    }
}

0 个答案:

没有答案