C#.net中的列表框选择 [英] Listbox Selection in C#.net

查看:97
本文介绍了C#.net中的列表框选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

li_Make.Items.FindByText(Maruti Suzuki).Selected = true;



li_make是一个列表框,其上有汽车制作绑定...当我尝试以上述方式找到一个make我得到的对象引用未找到错误...只有当传递给findbytext函数的字符串包含空格或连字符时才会出现错误...我怎么能避免这个...谢谢你的提前评论

li_Make.Items.FindByText("Maruti Suzuki").Selected = true;

li_make is a listbox with Car makes binded on it...WHen i try to find a make in the above way i get the object reference not found error...error occurs only when the string passed to the findbytext function contains space or hyphen...how can i avoid this...thank you for your comments in advance

推荐答案





您需要测试
Hi,

You need to test if
li_Make.Items.FindByText("Maruti Suzuki")

之前为空使用其中一个属性。



如果使用对象属性且对象为null,则会抛出未找到的异常对象引用。



为避免抛出异常,您可以在使用对象之前进行测试,例如以这种方式:



is null before using one of its properties.

If you use an object property and the object is null then it will throw an exception object reference not found.

To avoid throwing the exception you could do a test before using your object, for example in that way:

if (li_Make.Items.FindByText("Maruti Suzuki")!= null)
    li_Make.Items.FindByText("Maruti Suzuki").Selected = true;





你可以在这里看到有关他的例外的详细信息:

http://msdn.microsoft.com/en-us/library/system.data.objectnotfoundexception.aspx [ ^ ]



Valery。



you can see details about his exception here:
http://msdn.microsoft.com/en-us/library/system.data.objectnotfoundexception.aspx[^]

Valery.


这篇关于C#.net中的列表框选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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