473,799 Members | 3,866 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

20 New Member
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??
Sep 8 '08 #1
0 1144

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1752
by: BrianDH | last post by:
Hi I would ask this questiopn on the NG ASP but it would be days before anyone might respond. I am using vb.net code behind to create buttons to add to a web form. After the buttons are created I get this error: ((Control '_ctl0' of type 'Button' must be placed inside a form tag with runat=server.)) My Code example:
0
1344
by: ViRi | last post by:
There was an error loading types from assembly 'path to the dll 'Missing definition for required runtime implemented delegate method.' I get this error message when trying to add a custom control created in Visual Studio 2003 into my Visual Studio 2005 IDE, any idea what i need to modify in the 2003 control to make it usable in 2005? I tried searching for the specific error, but 0 results. Thanks in advance,
7
1336
by: Rene | last post by:
We all know that we can't call custom methods or properties form generic type parameters (<T>) by default because the compiler will complain about his. For example, the following won't normally compile: T.MyCustomMethod() The compiler will tell me that it's not sure that the generic type parameters (<T>) will contain such method and will requires me to constrain the generic type parameters.
7
1429
by: moondaddy | last post by:
I'm reading the book Wrox-Professional C# 2005 and it's trying to demonstrate an anonymous method in an event handler. I would not do this in a real app, but am trying to understand how this works. They have a windows form with a button on it. Rather than wiring up the click event to a method, they just wire it up to some code like this: btnOne.Click += new EventHandler(lblInfo.Text = "Show some text.";); Doesn't look like a good...
0
1050
by: RickL | last post by:
Hi, I'm fairly new to web application development and I'm creating an application for camera control, which leads to the question. I want to create a button that can respond to mouse down and mouse up events on the client-side. For example, suppose you have a button to control the zoom lens on a camera. When you click down on the mouse, the button would send a Zoom In command. When you release the mouse, the button would send a...
3
1777
by: William Stacey [C# MVP] | last post by:
It would be handy to be able to ref "this" from inside an AM such as: (string s) { Console.Writeline(s); DoSomething(this); } So treating am like a method of a class (which it is). Currently we have no context to know so you have to pass that as state which seems kinda
2
1174
by: Bob | last post by:
What name does the compiler give to anonymous methods in the output assembly - or what is the "name pattern"? .... can't imagine it would be relevant to anything I do... just curious.
5
11724
by: =?Utf-8?B?RWl0YW4=?= | last post by:
Hello, I need to create a background thread and two (or more) options are available to me: 1. BackgroundWorker 2. Asynch delegate method and BeginInvoke What are the differences between them in performance, complexity etc.
2
2181
by: tshad | last post by:
I can do the following with Find: Racer theRacer2 = racers.Find(delegate(Racer racer) { return racer.Car == "Ferrari"; }); But I can't seem to do the same with BinarySearch, this: int iktr = racers.BinarySearch(delegate(Racer racer) { return racer.Car == "Ferrari"; });
0
9543
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10488
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7567
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6808
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5588
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.