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.