365bet在线手机版-det365下载-best365手机官方

为什么将ComboBox.SelectedValue设置为null会导致ArgumentNullException?

为什么将ComboBox.SelectedValue设置为null会导致ArgumentNullException?

为什么将SelectedValue的ComboBox设置为null会导致ArgumentNullException

只有当ComboBox实际上是表单的一部分时,才会发生异常。我可以将SelectedValue设置为各种没有意义的值或类型,但不能将其设置为null。

并不是SelectedValue不能成为null。实际上,在我试图将其设置为null时,它的值是null。

在我的实际代码中,这不会发生在构造函数中,而且我也不会将其设置为null。代码使用的变量恰好是null。在尝试设置null之前,我可以通过检查变量不是SelectedValue来修复它。但我不明白的是,为什么我不能将它设置为null值。

代码编辑:DataSource现在包含一个ValueMembers值实际上是null的项

代码语言:javascript运行复制using System.Collections.Generic;

using System.Windows.Forms;

public class Form1 : Form {

public Form1() {

var comboBox1 = new ComboBox();

Controls.Add(comboBox1);

comboBox1.ValueMember = "Key";

comboBox1.DisplayMember = "Value";

comboBox1.DataSource = new List {

new Record {Key = "1", Value = "One"},

new Record {Key = null, Value = "null"}

};

comboBox1.SelectedItem = null; // no problem

comboBox1.SelectedValue = ""; // no problem

comboBox1.SelectedValue = new object(); // no problem

comboBox1.SelectedValue = null; // ArgumentNullException!!

}

}

public class Record {

public string Key { get; set; }

public string Value { get; set; }

}

← 上一篇: 俊美无俦是什么意思
下一篇: oppo手机桌面布局自定义怎么设置 →

相关推荐

Windows沙盒无法初始化[修复]

Windows沙盒无法初始化[修复]

2025-07-11 13:30:44 阅读: 7617
男篮世界杯积分榜数据列表

男篮世界杯积分榜数据列表

2025-08-24 09:52:05 阅读: 1457
虫子:《英雄联盟》中虫子是什么?

虫子:《英雄联盟》中虫子是什么?

2025-07-31 18:26:53 阅读: 4310
32强沙场点兵

32强沙场点兵

2025-08-12 18:34:07 阅读: 5088
电视回看软件

电视回看软件

2025-08-11 04:27:09 阅读: 3721
民航飞机客舱训练设备

民航飞机客舱训练设备

2025-08-03 20:59:54 阅读: 6947
2024年最新钻石回收价格表

2024年最新钻石回收价格表

2025-06-30 11:15:38 阅读: 181