Reputation: 1466
Here is the situation:
http://i962.photobucket.com/albums/ae103/kashyaprakesh/misc/denominationwindow.jpg
I have text boxes to left which accepts denomination values and other text box to right gives the total value for eg. in 1000's label's left textbox if I put value as 5 then to right the value is 5000.
I used lostFocus event handler to do this, but is it necessary to do the lost focus event handler for each and every textbox? There will surely be an other way.
private void textBox6_Leave(object sender, EventArgs e)
{
MessageBox.Show(e.ToString());
if (textBox6.Text == "")
{
string y = "0";
textBox6.Text = y;
textBox8.Text = y;
}
else
{
textBox8.Text = populateTotalAmount(textBox6.Text, 1000);
}
textBox8.ReadOnly = true;
}
private string populateTotalAmount(string denominations, int value)
{
int totalVal = Int32.Parse(denominations) * value;
return totalVal.ToString();
}
I would like to create a generic event handler which works on LostFocus
event and also I need to pass different value(ie, 500,100, etc etc etc)
so that I can use that value to send it populateTotalAmount
function.
Upvotes: 0
Views: 1120
Reputation: 101140
Something like this (haven't tested it)
foreach (Control ctrl in Controls)
{
if (ctrl is TextBox && ctrl.Name.StartsWith("math"))
ctrl.Focused += OnFocus;
}
Upvotes: 0
Reputation: 244757
Assuming you are using WinForms, you can create a custom control has the multiplication value as a property and then add that control to the form.
Upvotes: 0
Reputation: 941287
There's no compelling reason to not just simply iterate the text boxes and calculate the result. This code runs at human time, she can't tell the difference between a microsecond and millisecond.
Now you can have one Leave event handler for all text boxes. TextChanged would work too, but is a wee disorienting.
Upvotes: 1