Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2005

Tip: Looking for answers? Try searching our database.

clone a control including it's events

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ren Reyes - 08 Feb 2005 21:39 GMT
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
PK - 09 Feb 2005 11:07 GMT
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

> 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
[quoted text clipped - 7 lines]
> tia,
> ren
Ren Reyes - 09 Feb 2005 17:55 GMT
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

> Use AddHandler:
Sunny - 09 Feb 2005 20:26 GMT
> Hi PK,
>
[quoted text clipped - 7 lines]
>
> > Use AddHandler:

try:

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

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

Sunny
Ren Reyes - 09 Feb 2005 22:57 GMT
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

> try:
>
[quoted text clipped - 4 lines]
>
> Sunny
Sunny - 10 Feb 2005 00:20 GMT
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();
        }
    }
}
Ren Reyes - 10 Feb 2005 19:13 GMT
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
Sunny - 11 Feb 2005 16:53 GMT
> Hi Sunny,
>
[quoted text clipped - 6 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.