gpt4 book ai didi

c# - 如何在文本框为空时输入默认值

转载 作者:行者123 更新时间:2023-11-30 14:12:20 25 4
gpt4 key购买 nike

我有这个方法,它在那些文本框SUM值,我想改进它,所以如果这些文本框empty 我想将 "0" 插入其中,但我不知道该往哪里插入以及究竟放了什么才能让它像我想要的那样工作。我想知道这个问题很长时间了,有人可以给我一些建议吗?

void vypocti_naklady()
{
double a, b,c,d,e,f,g,h;
if (
!double.TryParse(p_ub_s.Text, out a) ||
!double.TryParse(p_poj_s.Text, out b) ||
!double.TryParse(p_jin_s.Text, out c) ||
!double.TryParse(p_dop_s.Text, out d) ||
!double.TryParse(p_prov_s.Text, out e) ||
!double.TryParse(p_pruv_s.Text, out f) ||
!double.TryParse(p_rez_s.Text, out g) ||
!double.TryParse(p_ost_s.Text, out h)
)
{
naklady.Text = "0";
return;
}

naklady.Text = (a+b+c+d+e+f+g+h).ToString();
}

感谢大家的帮助和时间。

最佳答案

您可以制作一个文本框验证事件(因为如果为空,您只需要插入 0 并且不保留焦点),并为所有其他文本框订阅该文本框验证事件。

例如:您有 5 个文本框订阅(通过单击例如 textbox1 属性窗口|事件并双击已验证),对于其他文本框,将其已验证事件订阅到该文本框,然后在其中放入:

private void textBox1_Validated(object sender, EventArgs e)
{
if (((TextBox)sender).Text == "")
{
((TextBox)sender).Text = "0";
}
}

关于c# - 如何在文本框为空时输入默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17902882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com