如何检查列表框的特定项目是否具有值 [英] how to check that listbox's specific item have a value or not

查看:104
本文介绍了如何检查列表框的特定项目是否具有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查列表框的特定项目是否有值,如果条件允许但两者都用的少,我正在做这两个操作

how to check that listbox''s specific item have a value or not i am doing this two if conditions but both are use less how to do this

if (!String.IsNullOrEmpty(lbTaxRate.GetItemText(lbTaxRate.Items[0]))&& !String.IsNullOrEmpty(lbTaxName.GetItemText(lbTaxName.Items[0])))
                {
                    objProduct.Tax1Name = lbTaxName.GetItemText(lbTaxName.Items[0]);
                    objProduct.Tax1Rate = Convert.ToDecimal(lbTaxRate.GetItemText(lbTaxRate.Items[0]));
                }

                if (!String.IsNullOrEmpty(lbTaxRate.GetItemText(lbTaxRate.Items[1])) && !String.IsNullOrEmpty(lbTaxName.GetItemText(lbTaxName.Items[1])))
                {
                    objProduct.Tax2Name = lbTaxName.GetItemText(lbTaxName.Items[1]);
                    objProduct.Tax2Rate = Convert.ToDecimal(lbTaxRate.GetItemText(lbTaxRate.Items[1]));
                }



如您在上面的item [1] .. item [0]上看到的...我正在检查这不是一个正确的方法,所以我该怎么做



as you can see above item[1]..item[0]...i am checking this is not a right approch so how i can i do this

推荐答案

问题是不恰当的:一个列表项,即使它存在,也是某个对象.它确实存在,因此始终具有价值.它取决于值"的含义,因为存在值类型和引用类型,但是从ListBox的角度来看,这是类型为System.Object的对象,这意味着该项目引用一些东西.

同样,这取决于您所说的价值"是什么.显然,您使用解释为整数的字符串.然后,您可以将空字符串视为非值",或更普遍的是,如果decimal.TryParse(string, out decimal)返回false,则可以将其视为非值".请参阅: http://msdn.microsoft.com/zh-CN /library/9zbda557%28v=vs.110%29.aspx [ combobox.selectedvalue在Winform中显示{}. a> [ ^ ] .

不要在需要数据本身的地方使用表示数据的字符串.

-SA
The question is is ill-posed: a list item, it it even exist, is some object. It does exist and, hence, always has a value. It depends on what do you mean by "value", because there are value types and reference types, but, from the point of your of the ListBox, this is an object of the type System.Object, which implies that the item reference something.

Again, it depends on what do you mean by "value". Apparently, you work with strings which you interpret as integers. Then you can consider empty string as "not a value", or, more generally, if decimal.TryParse(string, out decimal) returns false, you can consider this as "not a value". Please see: http://msdn.microsoft.com/en-us/library/9zbda557%28v=vs.110%29.aspx[^].

However, the whole approach is wrong.

You should not work with strings representing data instead of data itself. ListBox elements could be of any type, and you should use this fact by storing actual data in it. This is explained in detail in my past answer: combobox.selectedvalue shows {} in winform[^].

Don''t use strings representing data where you need the data itself.

—SA


我会同时为objProduct.TaxNameobjProduct.TaxRate使用数组(或者最好是"Tax"类实例的单个数组) ).这样,我就不必重复代码.
I would use arrays for both objProduct.TaxName and objProduct.TaxRate (or better a single array of a ''Tax'' class instances). This way I wouldn''t have to duplicate the code.


这篇关于如何检查列表框的特定项目是否具有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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