我如何在c#中操作listBox操作 [英] How do I operate listBox operations in c#

查看:170
本文介绍了我如何在c#中操作listBox操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我对listBoxes有疑问。在我的项目中有两个listBox。我在这些ListBox上更新,添加,删除,清理操作。其中一个没有问题,但另一个给出,

PresentationFramework.dll中出现未处理的类型'System.NullReferenceException'异常

附加信息:对象引用不是设置为一个对象的实例



错误,使用相同的代码。



例如当我想要的时候清除listBox中的所有项目,我这样做;



lboxCity.Items.Clear();并且它给出了上面提到的错误。

当我调试代码时,我认识到代码lboxCity.Items.Clear();

编译器去

private void lboxCity_SelectionChanged(对象发送者,SelectionChangedEventArgs e){}

做一个里面的操作。



我也在运行listBox时尝试了相同的调试。

代码之后

lboxRegion.Items.Clear ();编译器不运行函数

private void lboxRegion_SelectionChanged(object sender,SelectionChangedEventArgs e)



这些只是我的观察,我不确定问题在哪里。



如果不清楚请注意



摘要,同时操作列表框操作,它给出了nullReferencesException。

Hi everyone,

I have a problem about listBoxes. In my project there are two listBoxes. I do update, add,delete,clean operations on these ListBoxes. There is no problem with one of them but the other gives,
An unhandled exception of type 'System.NullReferenceException' occurred in PresentationFramework.dll
Additional information: Object reference not set to an instance of an object

error, with same codes.

For example when i want to clear all items in listBox, i do ;

lboxCity.Items.Clear(); and it gives the error mentioned above.
When i debug the code i recognize that after the code lboxCity.Items.Clear();
compiler goes
private void lboxCity_SelectionChanged(object sender, SelectionChangedEventArgs e){}
an do the operations inside.

Also I tried the same debug in running listBox.
After the code
lboxRegion.Items.Clear(); compiler does not run the function
private void lboxRegion_SelectionChanged(object sender, SelectionChangedEventArgs e)

These are just my observation, i am not sure where is the problem.

If it is not clear please note

Summary, while operating listbox operations, it gives nullReferencesException.

推荐答案

对于lboxCity.Items.Clear();使用空引用错误失败的方法意味着没有创建lboxCity,或者您已经删除了控件,或者您已经为名称'lboxCity指定了其他内容,例如'null。



在执行lboxCity.Items.Clear()的行上的代码中放置一个断点。当应用程序停在那里时,通过将鼠标悬停在它上来检查lboxCity的值。



第二个错误:一旦清除了ListBox的Items集合,为什么如果SelectionChanged事件发生了:没有什么可以选择的。
For the lboxCity.Items.Clear(); method to fail with a null reference error means lboxCity was not created, or you have done something to delete the Control, or you have assigned something else, like 'null, to the name 'lboxCity.

Put a break-point in your code on the line where you execute lboxCity.Items.Clear(); and when the application stops there, examine the value of lboxCity by hovering the mouse over it.

The second error: once you have cleared the Items collection of a ListBox why should the SelectionChanged Event ever fire: there's nothing there to select.


你好,



没有人可以克服问题。我很迷茫。而代码

lboxCity.Items.Clear();执行系统给出nullreferencesexception。

请注意,在执行代码之前,lBoxCity已经填满。



谢谢
Hi there,

Is not there anyone that can overcome the problem. I am very confused. While the code
lboxCity.Items.Clear(); is executed system give nullreferencesexception.
and please note that, just before executing the the code lBoxCity is filled.

Thanks


这篇关于我如何在c#中操作listBox操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆