且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

退出应用程序时组合框的空指针

更新时间:2022-06-27 00:20:41

首先,您如何将ComboBox.SelectedIndex设置为null?

它是一个int变量,它是不可为null的类型.

如果将其设置为null,那就是您的问题.

如果将其设置为-1,然后在SelectedIndex_Changed事件中出现错误,则需要首先检查SelectedIndex = -1并退出该方法.
First of all, how in the world are you setting ComboBox.SelectedIndex to null?

It is an int variable which is a non-nullable type.

If you''re setting it to null, then that''s your problem.

And if you''re setting it to -1 and then getting an error in your SelectedIndex_Changed event, then you need to first check to see if SelectedIndex = -1 and exit the method.


检查SelectedIndex的值,如果它为null或-1,则跳过错误的代码.
Check the value of SelectedIndex and skip over the code that errors if it''s null or -1.


或者,创建一个表单级别布尔值以指示该表单正在关闭并进行设置.在表单的关闭事件中为true.然后,如果该变量为false,则仅在组合框的已更改事件处理程序中运行代码.
Alternatively, create a form level boolean to indicate that the form is closing and set it to true in the form''s closing event. Then only run the code in your combo box''s changed event handler if that variable is false.