473,396 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PROBLEM SOLVED-(HOWTO)Dynamically Disable/Enable Component events!

I know it works for basic widows form components. . .
Similar approach can be applied for other controls if this doesn't work . . .

Typical Usage:
=============================
ArrayList eventData = new ArrayList();
EventDescriptorCollection events = TypeDescriptor.GetEvents(myComponent);
foreach (System.ComponentModel.EventDescriptor myEvent in events)
{
//Unwire the events
EventDatum ed = EventDatum.Create(button1, myEvent);
if (ed == null) continue;
EventData.Add(ed);
ed.Unwire(myComponent);
}
//Do something with button1 here . . .

// now rewire . . .

foreach(EventDatum ed in EventData)
if (ed != null)
ed.Wire(myComponent);
EventData.Clear();
=============================
EventDatum listing (assumes project appropriate references)
=============================
public class EventDatum
{
private EventDescriptor _eventDesc;
private Delegate _event;
private static MethodInfo GetEventsMethod(Type objType)
{
MethodInfo mi = objType.GetMethod("get_Events",All);
if ((mi ==null)& (objType.BaseType!=null))
mi = GetEventsMethod(objType.BaseType);
return mi;
}

private static EventHandlerList GetEvents(object obj)
{
MethodInfo mi = GetEventsMethod(obj.GetType());
if (mi == null) return null;
return (EventHandlerList) mi.Invoke(obj, new object[]{});
}

private static FieldInfo GetEventIDField(Type objType, string eventName)
{
FieldInfo fi = objType.GetField("Event"+eventName,All);
if ((fi ==null)& (objType.BaseType!=null))
fi = GetEventIDField(objType.BaseType, eventName);
return fi;
}

private static object GetEventID(object obj, string eventName)
{
FieldInfo fi = GetEventIDField(obj.GetType(), eventName);
if (fi ==null) return null;
return fi.GetValue(obj);
}

private static BindingFlags All
{
get
{
return
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance| BindingFlags.IgnoreCase|
BindingFlags.Static;
}
}

internal EventDatum(EventDescriptor desc, Delegate aEvent)
{
_eventDesc = desc;
_event = aEvent;
}

public static EventDatum Create(object obj, EventDescriptor desc)
{
EventHandlerList list = GetEvents(obj);
if (list==null) return null;
object key = GetEventID(obj, desc.Name);
if (key==null) return null;
Delegate evnt = list[key];
if (evnt == null) return null;
return new EventDatum(desc, evnt);
}

public void Wire(object obj)
{
_eventDesc.AddEventHandler(obj, _event);
}

public void Unwire(object obj)
{
_eventDesc.RemoveEventHandler(obj, _event);
}
}
Jul 21 '05 #1
0 981

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Freek te Water | last post by:
Hi, Hope no-one is offended by my probably noob question... Context: I have a web page design, which always centres in the middle of the screen (using a 100%*100% HTML-table). Now I also use...
2
by: Eric | last post by:
I'm trying to dynamically load a user control using on the .NET framework (not Visual Studio). The control was designed in Visual Studio and is named: Disable.ascx The first line is: <%@...
1
by: José Joye | last post by:
Hello, I'm playing around with dynamically loading user controls ...and having problems I created a really simple userControl (in fact contains a plain text box) and placed it into the...
2
by: ezmiller | last post by:
Hi, I am trying to learn how to dynamically write HTML using the W3C DOM. But I am having trouble with the code. It doesn't seem to be doing what I expect it to and I am not sure why. Let me...
2
by: ezmiller | last post by:
Hi, I have some code (which I will paste in below) that writes out some HTML dynamically using the W3C DOM...the last part of the code write out a simple table. MY problem is that the table is...
0
by: Gregg | last post by:
I'm having a problem dynamically appling cell text values in a table. The problem occurs when I try to set the cell text value in any cell other than the first cell of the frist row in the table....
1
by: James Black | last post by:
You can see the program at: http://dante.acomp.usf.edu/CommunicationSimulator/index.php I am trying to dynamically add script tags to my webpage, but I get an error that the first javascript...
11
by: onkar.n.mahajan | last post by:
Is it possible to from function call on fly in C programming language ? I am faced with an interesting problem in that I need to take function name and arguments on fly (actually from Database...
2
by: Nick Keighley | last post by:
On 20 Jun, 10:29, onkar.n.maha...@gmail.com wrote: no C is statically compiled. By the time it executes everything must be there and types are fixed. Dynamically linked libraries can...
3
by: jubergolandaj | last post by:
I am working on Outlook Addin developed in C# and VS-2008. In this we are having our own custom form .oft On this form we have our custom “Send” button on click event of it our processing continues....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.