How to enter a default value when a TextBox is empty
Solution 1
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.
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:
private void textBox1_Validated(object sender, EventArgs e)
{
if (((TextBox)sender).Text == "")
{
((TextBox)sender).Text = "0";
}
}
Solution 2
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)
This will count any invalid input as 0
Solution 3
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:
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.
Solution 4
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.
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.
Marek
Yo, I am currently playing around with bitcoin protocol and a platform for building decentralized applications named Ethereum. I enjoy developing variety applications under .NET platform, mainly in C# with SQL - based database systems. Wondering why nobody answers your question? Bitcoin tips are welcome to my jar: 1KAqjkCjQiWYnpjmNWWb7zsDGJEFLVbUDQ
Updated on July 09, 2022Comments
-
Marek almost 2 years
I have this
method
whichSUM
values in thosetextboxes
, I wanted to improve it so if any of thesetextboxes
isempty
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 ?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.