【问题标题】:InvalidArgument=Value '18' is invalid to 'SelectedIndex' Parameter name: SelectedIndexInvalidArgument=值 '18' 对 'SelectedIndex' 无效 参数名称:SelectedIndex
【发布时间】:2016-11-18 10:06:23
【问题描述】:

我有一个Button,我可以在其中将数据保存到数据库中。为此,我将获得ComboboxTextbox 上的信息。我在Combobox 中收集了一组项目,目前无法更改。

现在,我有 17 个项目,每次我保存一些东西时,它都会使用 cmbID.SelectedIndex += 1; 拉到下一个项目,但每次我从 Combobox 拉到最后一个项目时,它都会给我一个错误:

InvalidArgument=值 '18' 对 'SelectedIndex' 无效参数名称:SelectedIndex

为了解决这个问题,我尝试使用 if 语句:

if (cmbID.SelectedIndex >= 18)
{
    cmbID.SelectedIndex = 1;
}

但这不起作用,基本上如果Combobox达到'18',它应该回到我选择的SelectedIndex

你们知道我可以解决这个问题吗?

【问题讨论】:

    标签: c#


    【解决方案1】:

    组合框的索引不能超过 (item count -1),所以

    if(cmbID.SelectedIndex == (cmbID.Items.Count - 1))
    {
        cmbID.SelectedIndex =1;
    }else
    {
        cmbID.SelectedIndex += 1;
    }
    

    【讨论】:

    • @CodeCaster 现在可以了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多