Gustavo

Gustavo

  • NA
  • 1.3k
  • 454k

How do I: remove/replace redundant code

Mar 10 2010 2:03 PM


Hello:
 
I have many forms that have the same code that I would like to put in one place and access it from all the forms. An example of the code is listed below:
 
public void textBox_Enter(object sender, EventArgs e)
{
((
TextBox)sender).BackColor = Color.Ivory;
toolStripStatusLabelHint.Text =
"Hint: " + ((TextBox)sender).Name;
//... and more code here
}
 
How can I put this in one place and then have a one line code to have it include the statements?

 
Also I have this code that I would need to put in one place.
InitializeComponent();
//------------------------------------------------------------
string d = "";
string e = "";
Control c = GetNextControl(null, true);
while (c != null)
{
if (c.GetType().Name == "TextBox")
{
c.Enter +=
new EventHandler(textBox_Enter);
c.Leave +=
new EventHandler(textBox_Leave);
}
c = GetNextControl(c,
true);
}
//------------------------------------------------------------
.....

Answers (3)