C# 如何在 TextBox 为空时输入默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17902882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enter a default value when a TextBox is empty
提问by Marek
I have this method
which SUM
values in those textboxes
, I wanted to improve it so if any of these textboxes
is empty
I would like to insert into it "0"
but I didn't knew where to and what exactly put to make it work like I want. I have wondered about this for quiet long time, would someone suggest me something please ?
我有method
这些SUM
价值textboxes
,我想改进它,所以如果其中任何一个textboxes
是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();
}
Thanks everyone for their help and time.
感谢大家的帮助和时间。
采纳答案by terrybozzio
You can make one textbox validated event (because if empty you just need to insert 0 and not retain focus),and subscribe all other textboxes to that textbox validated event.
您可以创建一个文本框验证事件(因为如果为空,您只需要插入 0 而不是保持焦点),并将所有其他文本框订阅到该文本框验证事件。
For example: you have 5 textboxes subscribe (by clicking for example textbox1 properties window|events and double-click validated), and for the other textboxes subscribe their validated event to that one, then inside it put this:
例如:您有 5 个文本框订阅(例如,通过单击 textbox1 属性窗口|事件并双击已验证),对于其他文本框,将其验证事件订阅到该事件,然后在其中输入:
private void textBox1_Validated(object sender, EventArgs e)
{
if (((TextBox)sender).Text == "")
{
((TextBox)sender).Text = "0";
}
}
回答by TGH
private double GetValue(string input)
{
double val;
if(!double.TryParse(input,out val))
{
return val;
}
return 0;
}
var sum = this.Controls.OfType<TextBox>().Sum(t => GetValue(t.Text));
Try the above. Just run an OfType on the parent of the text boxes (parent could be the form itself)
试试上面的。只需在文本框的父级上运行 OfType(父级可以是表单本身)
This will count any invalid input as 0
这会将任何无效输入计为 0
回答by sircodesalot
Try this:
尝试这个:
// On the textboxes you want to monitor, attach to the "LostFocus" event.
textBox.LostFocus += textBox_LostFocus;
This monitors for when the TextBox has lost focus (has been clicked away from). When it has, then run this code:
这会监视 TextBox 何时失去焦点(已被单击离开)。当它有时,然后运行此代码:
static void textBox_LostFocus(object sender, EventArgs e) {
TextBox theTextBoxThatLostFocus = (TextBox)sender;
// If the textbox is empty, zeroize it.
if (String.IsNullOrEmpty(theTextBoxThatLostFocus.Text)) {
theTextBoxThatLostFocus.Text = "0";
}
}
If effect you watch the TextBox.LostFocus
event. Then when the use clicks away from the box, it will run textBox_LostFocus
. If the TextBox
is empty, then we replace the value with a zero.
如果效果你观看TextBox.LostFocus
事件。然后当用户点击远离盒子时,它会运行textBox_LostFocus
。如果TextBox
为空,则我们用零替换该值。
回答by Alejandro
An alternative would be not to use the TextBox
text directly and parse it, but to databind to a property and use those instead. The Binding
itself will do the parse and validation, leaving your variable always clean and ready for use.
另一种方法是不TextBox
直接使用文本并解析它,而是将数据绑定到属性并使用它们。在Binding
本身会做解析和验证,让你的变量总是干净,以备使用。
public partial class Form1 : Form
{
// Declare a couple of properties for receiving the numbers
public double ub_s { get; set; }
public double poj_s { get; set; } // I'll cut all other fields for simplicity
public Form1()
{
InitializeComponent();
// Bind each TextBox with its backing variable
this.p_ub_s.DataBindings.Add("Text", this, "ub_s");
this.p_poj_s.DataBindings.Add("Text", this, "poj_s");
}
// Here comes your code, with a little modification
private void vypocti_naklady()
{
if (this.ub_s == 0 || this.poj_s == 0 /*.......*/)
{
naklady.Text = "0";
return;
}
naklady.Text = (this.ub_s + this.poj_s).ToString();
}
}
You just work with properties, already safely typed as double
and forget about formating and parsing. You may improve this by moving all that data to a ViewModel
class and putting the logic there. Ideally, you can apply the same idea to the output TextBox
by databinding to it too, but for that to work you must implement INotifyPropertyChanged
so the bindings know when to update the UI.
您只需使用已经安全输入为 as 的属性,double
而无需考虑格式化和解析。您可以通过将所有数据移动到一个ViewModel
类并将逻辑放在那里来改进这一点。理想情况下,您也可以TextBox
通过数据绑定将相同的想法应用于输出,但要使其工作,您必须实现INotifyPropertyChanged
绑定才能知道何时更新 UI。