clone a control including it's events

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

i have a textBox1 control and i'm handling it's double click event thru
textBox_DoubleClick(object sender, System.EventArgs e). now is there a way to
clone this control at runtime including it's event(s) so that when i
double-click the cloned control, textBox_DoubleClick() would get called?

my code can clone the control right now, but i don't know how to proceed
with cloning events. I was trying to use EventInfo and EventDescriptor but
both needs a reference to the delegate.

tia,
ren
 
Use AddHandler:

' Declare cloned control
Private WithEvents myControl As TextBox

' Create an instance of a TextBox & Add to control collection
myControl = New TextBox
Controls.Add(myControl)

' Create an event handler named TextBox_DoubleClick, it will be called
' when the DoubleClick event on the control is fired
AddHandler myControl.DoubleClick, AddressOf TextBox_DoubleClick
 
Hi PK,

I'm sorry I forgot to mention that I'm creating a generic control cloner
function, where the only parameter is a ref to the original control, which
means i have no idea about the name of the delegate that is hooked to the
original control's event.

thanks,
ren
 
Hi PK,

I'm sorry I forgot to mention that I'm creating a generic control cloner
function, where the only parameter is a ref to the original control, which
means i have no idea about the name of the delegate that is hooked to the
original control's event.

thanks,
ren

try:

Delegate[] handlers = myOrigControl.DoubleClick.GetInvocationList();

foreach (Delegate handler in handlers)
myNewControl.DoubleClick += handler;

Sunny
 
Hi Sunny,

I'm not sure if this is gonna work. Events doesn't have methods, and
GetInvocationList() is a method of a delegate. In fact, if I could only have
access to a valid delegate object. my problem would be solved.

Have you tried coding this way before?

Thanks,
Ren
 
Hmm, my confusion was, because I have used it ALWAYS from inside the
class, holding the event. There are some restrictions, so you can not
call GetInvocationList() from outside the class. Still, what you need is
doable, but all the controls should be derived, and in your derived
classes you can expose a (interface?) method to return the list. See the
sample below for an idea. Take care of the line wraps.

Also, I find this a very good article on the topic:
http://blog.monstuff.com/archives/000040.html

Sunny

using System;

namespace test
{
public delegate void CustEventHandler();
/// <summary>
/// Summary description for test.
/// </summary>
public class test
{
[STAThread]
public static void Main()
{
Child child1 = new Child();
child1.MyEvent += new CustEventHandler
(MyEventHandler);
child1.RiseAnEvent();

Child child2 = new Child();
child2.RiseAnEvent();

Delegate[] handlers = child1.GetEventHandlers();
foreach (Delegate handler in handlers)
child2.MyEvent += (CustEventHandler)handler;

child2.RiseAnEvent();
}

public static void MyEventHandler()
{
Console.WriteLine("Event fired!");
}
}

internal class Child
{
public event CustEventHandler MyEvent;

public void RiseAnEvent()
{
Console.WriteLine("Fireing event...");
if (this.MyEvent != null)
this.MyEvent();
}

public Delegate[] GetEventHandlers()
{
return this.MyEvent.GetInvocationList();
}
}
}
 
Hi Sunny,

Again, thanks for the help. The code you supplied is really good, but the
thing is I am trying to clone the standard controls, or simply, controls that
i didn't create.

i found an incomplete solution ("Clone control in run-time?") written by
someone from microsoft (Jeffrey Tan) and he seems to know how to do this.
Unfortunately, cloning the event was not elaborated. he might not be
monitoring this newsgroup anymore.

he explained the process of cloning the control and proceeded with saying...

"2. You also need to use TypeDescriptor.GetEvents and
TypeDescriptor.GetAttributes methods to copy the events and attributes. "

I've been playing around with this classes but haven't been lucky.

Thanks,
Ren
 
Ren said:
Hi Sunny,

Again, thanks for the help. The code you supplied is really good, but the
thing is I am trying to clone the standard controls, or simply, controls
that i didn't create.

i found an incomplete solution ("Clone control in run-time?") written by
someone from microsoft (Jeffrey Tan) and he seems to know how to do this.
Unfortunately, cloning the event was not elaborated. he might not be
monitoring this newsgroup anymore.

He is, he just answered to my other question.
he explained the process of cloning the control and proceeded with
saying...

"2. You also need to use TypeDescriptor.GetEvents and
TypeDescriptor.GetAttributes methods to copy the events and attributes. "

In that case you do not have a choice but to use reflection. I haven't done
that for events, but I have done this to access other private members of a
class. When I'm back in the office in Monday, I'll try to find and post a
sample code.
I've been playing around with this classes but haven't been lucky.

Thanks,
Ren

Cheers
Sunny
 
Back
Top