本文介绍了如何从文本框忽略空白字段的所有值总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Good day all, I need some help
I have 10 text boxes to add to get the total sum .. some thing like this
int totalQLoaded = int.Parse(Txt50ClCokeQuantityLoaded.Text) + int.Parse(Txt50ClFantaQuantityLoaded.Text) + int.Parse(Txt50ClSpriteQuantityLoaded.Text)
+ int.Parse(TxtBCokeQuantityLoaded.Text) + int.Parse(TxtBEvaQuantityLoaded.Text) +
//the calculation works fine if all the text boxes are filled with numbers.
but in a situation where some fields are left blank how do i get the total sum ignoring those field that are blank..
thanks for your time I appreciate
推荐答案
private int GetValue(string s)
{
int value = 0;
if (!string.IsNullOrWhiteSpace(s))
{
int.TryParse(s, out value);
}
return value;
}
然后你所要做的就是调用它:
Then all you have to do is call that instead:
int totalQLoaded = GetValue(Txt50ClCokeQuantityLoaded.Text) +
GetValue(Txt50ClFantaQuantityLoaded.Text) +
...
这篇关于如何从文本框忽略空白字段的所有值总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!