Connecting Tech Pros Worldwide Forums | Help | Site Map

C# :how to create a delegate to an anonymous(?) button method

Newbie
 
Join Date: Jul 2008
Posts: 20
#1: Sep 8 '08
Okay so I have a whole bunch of begininvokes to update my gui. I enable/disable, change the text, and all that beautiful stuff.

This is, unfortunately, how my code looks right now:

Expand|Select|Wrap|Line Numbers
  1.     public abstract class update
  2.     {
  3.         public delegate void button_Callback(object[] pass);        
  4.         public static void button(object[] pass)
  5.         {
  6.             Button button = (Button)pass[0];
  7.             string command = (string)pass[1];
  8.             switch (command)
  9.             {
  10.  
  11.                 case "enable":
  12.                     button.Enabled = (bool)pass[2];
  13.                     break;
  14.                 case "text":
  15.                     button.Text = (string) pass[2];       
  16.                     break;
  17.                 default:
  18.                     break;
  19.             }
  20.         }
  21. }
so when I want to update my gui I type:
Expand|Select|Wrap|Line Numbers
  1.  BeginInvoke(new update.button_Callback(update.button),new object[] { new object[] { btn_startspg, "enable", true}});
As you can see, the problem is passing the method name as a string and going through a switch.... a lot of hard-coding

I thought of these solutions but none enthuse me that much:

1) one method per method I want to invoke (doesn't really cut down on the coding)
2) create a child of a button and add a delegate for each signature (this would involve changing all my buttons to the child button)
3) append the button class with some delegates so I can directly invoke them (I could be happy with this but I'm not sure this will work)

There must be a brilliant way to do this.

Any suggestions??

Reply