First of all, you can't have put a double in the
Text
property, you need to put a string in it, for example
"0.0"
instead of
0.0
.
Also, instead of
ActiveControl
, use the
sender
attribute.
It is also better to use
String.IsNullOrEmpty
instead of
== String.Empty
because now you also check whether the string is null. And if you consider a whitespace-only string as 'empty', then use
IsNullOrWhiteSpace
.
Put the
y
variable outside the method; this avoids that it gets recreated every time the method gets called.
string[] y = new string[] { "txtA", "txtB", "txtC", "txtD"};
private void txtA_Leave(object sender, EventArgs e)
{
if (!(sender is TextBox))
{
return;
}
TextBox control = sender as TextBox;
if (y.Contains(control.Name) && String.IsNullOrEmpty(control.Text))
{
control.Text = 0.0;
control.TextAlign = HorizontalAlignment.Center;
}
}
More information about the
sender
attribute:
http://stackoverflow.com/questions/1303145/net-events-what-are-object-sender-eventargs-e[
^]