473,379 Members | 1,245 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,379 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 980

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....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.