views:

67

answers:

3

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...

below is the code i use

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....

can some one pls help me with this.. ill be really thankful.....

+1  A: 

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.

Hans Passant
A: 

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.

svick
A: 

Something like this (haven't tested it)

foreach (Control ctrl in Controls)
{
   if (ctrl is TextBox && ctrl.Name.StartsWith("math"))
      ctrl.Focused += OnFocus;
}
jgauffin