将标签汇总到另一个标签C# [英] Sum label to another label C#

查看:87
本文介绍了将标签汇总到另一个标签C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我错了什么?在哪里放这个代码?



我尝试更改选择,表单加载...但是不工作...



我想念的是什么?



帮助谢谢



我有什么试过:



What am I wrong? where to put this code?

I try in selection changed, form load...but dont work...

What I miss?

Help Thank you

What I have tried:

if (ukupnolabel.Text.Length > 0 && label3.Text.Length > 0)
{
label4.Text = Convert.ToDouble(Convert.ToDouble(ukupnolabel.Text) * Convert.ToDouble(label3.Text) / 100).ToString("#,0.00");
}

推荐答案

更好的是,为什么要使用控件的Text属性将数据传递给代码的其他部分?千万不要这样做!你这样做的事实就是说你的代码设计非常糟糕。



控件用于显示来自数据模型各部分的数据。您将数据传递给其他方法来执行操作,例如求和,从不控制显示数据。
Better yet, why are you passing data to other parts of your code using the Text properties of controls? NEVER DO THIS! The mere fact that you're doing this says your code is very badly designed.

Controls are for displaying data from parts of your data model. You pass your data to other methods to do stuff, like summing, NEVER CONTROLS THAT DISPLAY THAT DATA.


不要在用户输入上使用转换:请改用TryParse。当用户输入错误时,转换会抛出异常,TryParse允许您优雅地响应用户并让他们解决问题。

Don't use Convert on user input: use TryParse instead. Convert throws an exception when the user mistypes, TryParse lets you respond gracefully to the user and let them fix the problem.
double ukupno;
if (!double.TryParse(ukupnolabel.Text, out ukupno))
   {
   ... report problem to user ...
   return;
   }
double lab3;
if (!double.TryParse(label3.Text, out lab3))
   {
   ... report problem to user ...
   return;
   }
label4.Text = (ukupno * lab3 / 100.0).ToString("#,0.00");


这篇关于将标签汇总到另一个标签C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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