I am adding a handler to textbox using the following code:
private void frmLogin_Load(object sender, EventArgs e)
{
foreach (Control tb in this.Controls)
{
if (tb is TextBox)
{
TextBox tb1 = (TextBox)tb;
tb1.KeyDown += new KeyEventHandler(TextBox_KeyDown);
}
}
}
I am also removing handler using the following code:
private void frmLogin_FormClosed(object sender, FormClosedEventArgs e)
{
foreach (Control tb in this.Controls)
{
if (tb is TextBox)
{
TextBox tb1 = (TextBox)tb;
tb1.KeyDown -= new KeyEventHandler(TextBox_KeyDown);
}
}
}
Is the correct way or is there a better alternative?
It is good, but you dont need to remove the handler, and adding the handler just put this:
because
new KeyEventHandler(TextBox_KeyDown);is redundant.