如何从列表框中添加小数值? [英] How do I add decimal values from a listbox?

查看:99
本文介绍了如何从列表框中添加小数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候其他程序员...我是一名业余程序员,我的问题是在我的LISTBOX中我有数字项目,例如R699.99,R399.99等。现在我如何添加它们以显示我的txtTolalAmount当我计算?...请帮助

解决方案

ListBox 项目的正确设计是混淆的主题许多。正确的方法是:如果您需要使用项目中存储的数据进行操作,请不要存储字符串,存储数据。然后,将这些项目的正确表示视为UI中显示的字符串。



我解释了 System.Windows.Forms <的解决方案/ code>在过去的答案中详细说明: combobox.selectedvalue显示{} in winform [ ^ ]。



对于WPF,方法可以完全相同,但许多细节可能不同。你真的需要指定你真正想要的 ListBox 或任何其他.NET FCL类 - 请参阅我对该问题的评论。



-SA


首先,请仔细阅读谢尔盖的回答。



其次,最简单的方法之一就是使用 Linq [ ^ ]。



  var  total = ListBox1.Items 
.Sum(x = > decimal )x.Value.Substring( 2 10 ));
TextBox1.Text = total.ToString();





注意:未经测试,但它也可以正常工作。



如需了解更多信息,请参阅: LINQ [ ^ ]


Greeting fellow programmers...I'm an amateur programmer and my problem is that in my LISTBOX I have numeric items e.g R699.99, R399.99, etc. Now how do I add them up to display im my txtTolalAmount when I calc?...please help

解决方案

This right design of ListBox items is the topic which confuses many. The right recipe is: if you need to operate with the data stored in items, don't store strings, store your data. And then, care of right representation of these items as the strings shown in UI.

I explained the solution for System.Windows.Forms in detail in this past answer: combobox.selectedvalue shows {} in winform[^].

For WPF, the approach can be exactly the same, but many details can be different. You really need to specify which ListBox or any other .NET FCL class you really mean — see also my comment to the question.

—SA


First of all, please, read Sergey's answer very carefully.

Second of all, one of the easiest way to get total is to use Linq[^].

var total = ListBox1.Items
    .Sum(x=>(decimal)x.Value.Substring(2,10));
TextBox1.Text = total.ToString();



Note: not tested, but it should work as well.

For further information, please see: Introduction to LINQ[^]


这篇关于如何从列表框中添加小数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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