473,756 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to clone event handlers

Hello

It seems that I should implement ICloneable to implement my own clone
object. the critical point for me is to make a control object based on
another control object that all of its event handlers are set like the old
one. Is there a way to do this job?

For example, is there a way to use EventInfo object to get all event
handlers of the old control in runtime and set my new cloned control events
to the event handlers of the old control?

Any suggestion is appreciated.

Regards
Hamed
Hello

I am developing a utility to be reused in other programs. It

I have an object of type Control (a TextBox, ComboBox, etc.) that other
programmers use it in applications. they may set some of properties or
assign event handlers. I need to be able to clone the manipulated control
at runtime.

I could use the Clone method of some objects (like Font, Style, String,
etc..) but the controls like Button, TextBox, ListBox doesn't have this
kind
of method.
How can I create a clone of an object instance for controls like TextBox,
ListBox, ListViews, CheckBox ??
Any help is appreciated,

Aug 12 '06
14 8637
I found the following link in the net. But it seems it has left the solution
unfinished.

http://www.dotnet247.com/247referenc...58/293131.aspx

This message was discovered on microsoft.publi c.dotnet.langua ges.csharp.

the question is if we know that we want to find all subscribed methods of
Click event how could we do the job?

(Take a look to the following code)
public class MyTextBox : TextBox, ICloneable
{
public object Clone()
{
MyTextBox clone = new MyTextBox();
clone.Click += this.Click; //compiler error!
The event '....Click' can only appear on the left hand side of += or -=
}
}

"Hamed" <ha***@raymehr. comwrote in message
news:O7******** ******@TK2MSFTN GP04.phx.gbl...
Nicholas

The method that is creating the clone is a member of the class being
cloned. I want for example to assign the Click event of the control of my
own to the new cloned object. but when I tried your solution I got
compiler error. for example:

public class MyTextBox : TextBox, ICloneable
{
public object Clone()
{
MyTextBox clone = new MyTextBox();
clone.Click += this.Click; //compiler error!
The event '....Click' can only appear on the left hand side of += or -=
}
}

Would you kindly describe your suggestion more?

Best Regards
Hamed
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote
in message news:Ox******** ******@TK2MSFTN GP04.phx.gbl...
>Hamed,

Ultimately, you can't do it reliably.

If the method that is creating the clone is a member of the class
being cloned, then you can simply do:

// Inside clone method.
MyClass clone = new MyClass();
clone.MyEven t += this.MyEvent;

This assumes that you are declaring an event using the event keyword,
and not using add/remove handlers. It works because the C# compiler
creates a backing field of the same name (MyEvent).

However, if you declare your event like this:

private EventHandler myEventHandler;

public event EventHandler MyEvent
{
add
{
myEventHandler += value;
}
remove
{
myEventHandler -= value;
}
}

Then the call to assign the event will fail, because the compiler will
not see the backing field that has the delegate in it, and revert to
that.

Even if you are making the call from outside the type, the same
problem exists, since there is nothing on the metadata that links the
event to the backing field. There is nothing in the metadata that says
that the backing field has to be connected to the event. It's like
asking for metadata on a property, and then asking what the backing field
is (or multiple backing fields, since properties can really return
anything they want, just like event handlers).

So, that being said, there really isn't a reliable way to do this.
The best you could do is have an interface (if you plan on doing this
across multiple types) which would return a mapping of events to
handlers, and then when you clone the object, you would get those
delegates and then perform the clone.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

Aug 12 '06 #11
Hamed,

You can't copy the events reliably. If you look at the definition of
the Click event in the TextBoxBase class, you will see the reason it fails
when you try and assign the event to the new clone (it defines custom event
handlers).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Hamed" <ha***@raymehr. comwrote in message
news:O7******** ******@TK2MSFTN GP04.phx.gbl...
Nicholas

The method that is creating the clone is a member of the class being
cloned. I want for example to assign the Click event of the control of my
own to the new cloned object. but when I tried your solution I got
compiler error. for example:

public class MyTextBox : TextBox, ICloneable
{
public object Clone()
{
MyTextBox clone = new MyTextBox();
clone.Click += this.Click; //compiler error!
The event '....Click' can only appear on the left hand side of += or -=
}
}

Would you kindly describe your suggestion more?

Best Regards
Hamed
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote
in message news:Ox******** ******@TK2MSFTN GP04.phx.gbl...
>Hamed,

Ultimately, you can't do it reliably.

If the method that is creating the clone is a member of the class
being cloned, then you can simply do:

// Inside clone method.
MyClass clone = new MyClass();
clone.MyEven t += this.MyEvent;

This assumes that you are declaring an event using the event keyword,
and not using add/remove handlers. It works because the C# compiler
creates a backing field of the same name (MyEvent).

However, if you declare your event like this:

private EventHandler myEventHandler;

public event EventHandler MyEvent
{
add
{
myEventHandler += value;
}
remove
{
myEventHandler -= value;
}
}

Then the call to assign the event will fail, because the compiler will
not see the backing field that has the delegate in it, and revert to
that.

Even if you are making the call from outside the type, the same
problem exists, since there is nothing on the metadata that links the
event to the backing field. There is nothing in the metadata that says
that the backing field has to be connected to the event. It's like
asking for metadata on a property, and then asking what the backing field
is (or multiple backing fields, since properties can really return
anything they want, just like event handlers).

So, that being said, there really isn't a reliable way to do this.
The best you could do is have an interface (if you plan on doing this
across multiple types) which would return a mapping of events to
handlers, and then when you clone the object, you would get those
delegates and then perform the clone.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Hamed" <ha*******@yaho o.comwrote in message
news:uF******* *******@TK2MSFT NGP03.phx.gbl.. .
>>Hello

It seems that I should implement ICloneable to implement my own clone
object. the critical point for me is to make a control object based on
another control object that all of its event handlers are set like the
old one. Is there a way to do this job?

For example, is there a way to use EventInfo object to get all event
handlers of the old control in runtime and set my new cloned control
events to the event handlers of the old control?

Any suggestion is appreciated.

Regards
Hamed

Hello

I am developing a utility to be reused in other programs. It

I have an object of type Control (a TextBox, ComboBox, etc.) that other
programmer s use it in applications. they may set some of properties or
assign event handlers. I need to be able to clone the manipulated
control at runtime.

I could use the Clone method of some objects (like Font, Style, String,
etc..) but the controls like Button, TextBox, ListBox doesn't have this
kind
of method.
How can I create a clone of an object instance for controls like
TextBox,
ListBox, ListViews, CheckBox ??
Any help is appreciated,





Aug 12 '06 #12
Hamed,

Actually, this thread and that thread are pretty much exactly the same.
There is no reliable way to do this.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Hamed" <ha***@raymehr. comwrote in message
news:uL******** ******@TK2MSFTN GP05.phx.gbl...
>I found the following link in the net. But it seems it has left the
solution unfinished.

http://www.dotnet247.com/247referenc...58/293131.aspx

This message was discovered on microsoft.publi c.dotnet.langua ges.csharp.

the question is if we know that we want to find all subscribed methods of
Click event how could we do the job?

(Take a look to the following code)
>public class MyTextBox : TextBox, ICloneable
{
public object Clone()
{
MyTextBox clone = new MyTextBox();
clone.Click += this.Click; //compiler
error! The event '....Click' can only appear on the left hand side of +=
or -=
}
}


"Hamed" <ha***@raymehr. comwrote in message
news:O7******** ******@TK2MSFTN GP04.phx.gbl...
>Nicholas

The method that is creating the clone is a member of the class being
cloned. I want for example to assign the Click event of the control of my
own to the new cloned object. but when I tried your solution I got
compiler error. for example:

public class MyTextBox : TextBox, ICloneable
{
public object Clone()
{
MyTextBox clone = new MyTextBox();
clone.Click += this.Click; //compiler
error! The event '....Click' can only appear on the left hand side of +=
or -=
}
}

Would you kindly describe your suggestion more?

Best Regards
Hamed
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote
in message news:Ox******** ******@TK2MSFTN GP04.phx.gbl...
>>Hamed,

Ultimately, you can't do it reliably.

If the method that is creating the clone is a member of the class
being cloned, then you can simply do:

// Inside clone method.
MyClass clone = new MyClass();
clone.MyEve nt += this.MyEvent;

This assumes that you are declaring an event using the event keyword,
and not using add/remove handlers. It works because the C# compiler
creates a backing field of the same name (MyEvent).

However, if you declare your event like this:

private EventHandler myEventHandler;

public event EventHandler MyEvent
{
add
{
myEventHandler += value;
}
remove
{
myEventHandler -= value;
}
}

Then the call to assign the event will fail, because the compiler
will not see the backing field that has the delegate in it, and revert
to that.

Even if you are making the call from outside the type, the same
problem exists, since there is nothing on the metadata that links the
event to the backing field. There is nothing in the metadata that says
that the backing field has to be connected to the event. It's like
asking for metadata on a property, and then asking what the backing
field is (or multiple backing fields, since properties can really return
anything they want, just like event handlers).

So, that being said, there really isn't a reliable way to do this.
The best you could do is have an interface (if you plan on doing this
across multiple types) which would return a mapping of events to
handlers, and then when you clone the object, you would get those
delegates and then perform the clone.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m


Aug 13 '06 #13
Nicholas,

I appreciate your comments. thanks to pay attention. but:

When you say there is no reliable way, does this mean there may be some
unreliable ways?

Hamed
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:ez******** *****@TK2MSFTNG P05.phx.gbl...
Hamed,

Actually, this thread and that thread are pretty much exactly the same.
There is no reliable way to do this.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Hamed" <ha***@raymehr. comwrote in message
news:uL******** ******@TK2MSFTN GP05.phx.gbl...
>>I found the following link in the net. But it seems it has left the
solution unfinished.

http://www.dotnet247.com/247referenc...58/293131.aspx

This message was discovered on microsoft.publi c.dotnet.langua ges.csharp.

the question is if we know that we want to find all subscribed methods of
Click event how could we do the job?

(Take a look to the following code)
>>public class MyTextBox : TextBox, ICloneable
{
public object Clone()
{
MyTextBox clone = new MyTextBox();
clone.Click += this.Click; //compiler
error! The event '....Click' can only appear on the left hand side of +=
or -=
}
}


"Hamed" <ha***@raymehr. comwrote in message
news:O7******* *******@TK2MSFT NGP04.phx.gbl.. .
>>Nicholas

The method that is creating the clone is a member of the class being
cloned. I want for example to assign the Click event of the control of
my own to the new cloned object. but when I tried your solution I got
compiler error. for example:

public class MyTextBox : TextBox, ICloneable
{
public object Clone()
{
MyTextBox clone = new MyTextBox();
clone.Click += this.Click; //compiler
error! The event '....Click' can only appear on the left hand side of +=
or -=
}
}

Would you kindly describe your suggestion more?

Best Regards
Hamed
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote
in message news:Ox******** ******@TK2MSFTN GP04.phx.gbl...
Hamed,

Ultimately, you can't do it reliably.

If the method that is creating the clone is a member of the class
being cloned, then you can simply do:

// Inside clone method.
MyClass clone = new MyClass();
clone.MyEven t += this.MyEvent;

This assumes that you are declaring an event using the event
keyword, and not using add/remove handlers. It works because the C#
compiler creates a backing field of the same name (MyEvent).

However, if you declare your event like this:

private EventHandler myEventHandler;

public event EventHandler MyEvent
{
add
{
myEventHandler += value;
}
remove
{
myEventHandler -= value;
}
}

Then the call to assign the event will fail, because the compiler
will not see the backing field that has the delegate in it, and revert
to that.

Even if you are making the call from outside the type, the same
problem exists, since there is nothing on the metadata that links the
event to the backing field. There is nothing in the metadata that says
that the backing field has to be connected to the event. It's like
asking for metadata on a property, and then asking what the backing
field is (or multiple backing fields, since properties can really
return anything they want, just like event handlers).

So, that being said, there really isn't a reliable way to do this.
The best you could do is have an interface (if you plan on doing this
across multiple types) which would return a mapping of events to
handlers, and then when you clone the object, you would get those
delegates and then perform the clone.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m



Aug 14 '06 #14
Thanks for your kind attention.

Of course, I found some solutions using Reflection. It works perfectly but
the solution is messy

Thanks a lot and Regards.

-----Original Message-----

From: Nicholas Paldino [.NET/C# MVP] [mailto:ca****** *@caspershouse. com]

Sent: Sunday, August 13, 2006 5:19 PM

To: ha***@raymehr.c om

Subject: Re: How to clone event handlers

Hamed,

Actually, this thread and that thread are pretty much exactly the same.

There is no reliable way to do this.

--

- Nicholas Paldino [.NET/C# MVP]

- mv*@spam.guard. caspershouse.co m

"Hamed" <ha***@raymehr. comwrote in message
news:uL******** ******@TK2MSFTN GP05.phx.gbl...
>I found the following link in the net. But it seems it has left the
>solution unfinished.
>
http://www.dotnet247.com/247referenc...58/293131.aspx
>
This message was discovered on
microsoft.publi c.dotnet.langua ges.csharp.
>
the question is if we know that we want to find all subscribed methods
of
Click event how could we do the job?
>
(Take a look to the following code)
>public class MyTextBox : TextBox, ICloneable {
>public object Clone()
>{
>MyTextBox clone = new MyTextBox();
>clone.Click += this.Click; //compiler
>error! The event '....Click' can only appear on the left hand side of
+=
>or -=
>}
>}
>>
>
>
"Hamed" <ha***@raymehr. comwrote in message
news:O7******** ******@TK2MSFTN GP04.phx.gbl...
>Nicholas
>>
>The method that is creating the clone is a member of the class being
>cloned. I want for example to assign the Click event of the control
of my
>own to the new cloned object. but when I tried your solution I got
>compiler error. for example:
>>
>public class MyTextBox : TextBox, ICloneable {
>public object Clone()
>{
>MyTextBox clone = new MyTextBox();
>clone.Click += this.Click; //compiler
>error! The event '....Click' can only appear on the left hand side of
+=
>or -=
>}
>}
>>
>Would you kindly describe your suggestion more?
>>
>Best Regards
>Hamed
>>
>>
>"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om>
wrote
>in message news:Ox******** ******@TK2MSFTN GP04.phx.gbl...
>>Hamed,
>>>
>>Ultimately, you can't do it reliably.
>>>
>>If the method that is creating the clone is a member of the class
>>being cloned, then you can simply do:
>>>
>>// Inside clone method.
>>MyClass clone = new MyClass();
>>clone.MyEve nt += this.MyEvent;
>>>
>>This assumes that you are declaring an event using the event
keyword,
>>and not using add/remove handlers. It works because the C# compiler
>>creates a backing field of the same name (MyEvent).
>>>
>>However, if you declare your event like this:
>>>
>>private EventHandler myEventHandler;
>>>
>>public event EventHandler MyEvent
>>{
>>add
>>{
>>myEventHandle r += value;
>>}
>>remove
>>{
>>myEventHandle r -= value;
>>}
>>}
>>>
>>Then the call to assign the event will fail, because the compiler
>>will not see the backing field that has the delegate in it, and
revert
>>to that.
>>>
>>Even if you are making the call from outside the type, the same
>>problem exists, since there is nothing on the metadata that links
the
>>event to the backing field. There is nothing in the metadata that
says
>>that the backing field has to be connected to the event. It's like
>>asking for metadata on a property, and then asking what the backing
>>field is (or multiple backing fields, since properties can really
return
>>anything they want, just like event handlers).
>>>
>>So, that being said, there really isn't a reliable way to do
this.
>>The best you could do is have an interface (if you plan on doing
this
>>across multiple types) which would return a mapping of events to
>>handlers, and then when you clone the object, you would get those
>>delegates and then perform the clone.
>>>
>>Hope this helps.
>>>
>>>
>>--
>>- Nicholas Paldino [.NET/C# MVP]
>>- mv*@spam.guard. caspershouse.co m
>>>
>
>

Aug 15 '06 #15

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

Similar topics

10
3604
by: tony kulik | last post by:
This code works fine in ie and opera but not at all in Mozilla. Anybody got a clue as to how to get it right? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <script language="JavaScript" type="text/javascript"> function show(that) { if (box.style.visibility=='hidden') { that.style.visibility = 'visible'}; }
13
3518
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the following issues still exist. The article shows how to find methods on a receiver that match the pattern OnXXXX given the sender. It loops through the sender events and tries to get methods from the receiver that match the pattern. For each one...
0
1007
by: VBTricks.de.vu Webmaster | last post by:
Hello, I'm trying to clone a ToolStripMenuItem via creating a new instance and copying all properties, but how to copy the event handlers? Or is there an easier way? Thanks in advance, Stefan
16
2516
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some of properties or assign event handlers. I need to be able to clone the manipulated control at runtime. I could use the Clone method of some objects (like Font, Style, String,
14
3913
by: Ste | last post by:
Good morning, what can be the best way to clone an object with events ? This because I have made a deep copy of my object and added new event handlers but events are fired from the original instance, not in cloned instance. Thank you. Stefano.
0
1016
by: \(O\)enone | last post by:
I'm working on some code which dynamically adds WinForms controls to a form. It's all working well but I'm having to manually call AddHandler repeatedly for each event I am using each time I add new control to hook up all the event handlers. As I already have an initial control on my form with all the event handlers applied by the underlying source code, is there any way I can loop through all of the event handlers for that control and...
0
9455
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9271
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
10031
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...
1
9838
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9708
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7242
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
6534
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
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.