• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

Delegates everywhere. How to simplify this?

I have a Windows Form with text boxes and labels etc., and each one has it's own method for updating the text. Looks like a lot of duplicate code. Is there a way to simplify this?
delegate void UpdateTextBoxDelegate(string text);

/* Call this method to append text to textBox1 */
public void AppendTextBox1(string text)
{
  // Check if this method is running on a different thread
  if (this.textBox1.InvokeRequired)
  {
    // It's on a different thread, so use Invoke. (We call ourself through a delegate.)
    UpdateTextBoxDelegate d = new UpdateTextBoxDelegate(AppendTextBox1);
    this.textBoxLog.BeginInvoke (d, new object[] { text });
  }
  else
  {
    try
    {
      this.textBox1.AppendText(text + Environment.NewLine);
    }
    catch (Exception e)
    {
      LogMsg("AppendTextBox1: Exception: " + e.Message);
    }
  }
}

/* Call this method to append text to textBox2 */
public void AppendTextBox2(string text)
{
  // Check if this method is running on a different thread
  if (this.textBox2.InvokeRequired)
  {
    // It's on a different thread, so use Invoke. (We call ourself through a delegate.)
    UpdateTextBoxDelegate d = new UpdateTextBoxDelegate(AppendTextBox2);
    this.textBox2.BeginInvoke (d, new object[] { text });
  }
  else
  {
    try
    {
      this.textBox2.AppendText(text + Environment.NewLine);
    }
    catch (Exception e)
    {
      LogMsg("AppendTextBox2: Exception: " + e.Message);
    }
  }
}

Open in new window

and it goes on like that for
TextBox3
TextBox4
Label1
Label2
etc...
So much repeated code...
0
deleyd
Asked:
deleyd
  • 2
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Pass in a TextBox and a String so you can use a generic method:  
delegate void AppendTextBoxDelegate(TextBox textBox, string text);

        public void AppendTextBox(TextBox textBox, string text)
        {
            // Check if this method is running on a different thread
            if (textBox.InvokeRequired)
            {
                // It's on a different thread, so use Invoke. (We call ourself through a delegate.)
                AppendTextBoxDelegate d = new AppendTextBoxDelegate(AppendTextBox);
                textBox.BeginInvoke(d, new object[] { textBox, text });
            }
            else
            {
                try
                {
                    textBox.AppendText(text + Environment.NewLine);
                }
                catch (Exception e)
                {
                    LogMsg(textBox.Name + ": Exception: " + e.Message);
                }
            }
        }

Open in new window


Now you can call it like:

    AppendTextBox(textBox1, "Hello");

    AppendTextBox(textBox2, "World!");

0
 
Easwaran ParamasivamCommented:
Above method is again repetitive as Lable control required another method.

All controls such as Textbox, Lable are derived from base class Control. Hence pass Control as parameter to the method.

Say for example

public void  AppendTextBox(TextBox textBox, string text)

should be written as

public void  AppendTextBox(Control textBox, string text)

Now you could pass any of the control.



0
 
deleydAuthor Commented:
Love it! It just keeps getting better and better!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Right...he used AppendText() for the TextBox, which isn't available for all controls.  If you are only setting the Text() property then you can make it more generic as EaswaranP suggests.
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now