DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    John Guest

    Run time event handelers


    Can Event handlers be built to handle Objects that are created at runtime?

    Lets say I want the System.Windows.Forms.MonthCalendar() to be created and
    displayed at the click of a button. I can do this with out a problem. Here's
    the code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Handles Button1.Click
    Public cmd As New System.Windows.Forms.MonthCalendar()
    cmd = New System.Windows.Forms.MonthCalendar()
    cmd.TodayDate = "10/8/2001"
    cmd.Location = New Point(sender.location.x + sender.width, sender.location.y
    + sender.height)
    cmd.Name = "calPopUp"
    Controls.Add(cmd)
    cmd.BringToFront()
    End Sub

    Now I want an even handler to handle the doubleclick events for the calendar.
    Can this be done?
    I do not want to create a generic event handler that handles all DoubleClick
    Events just the DoubleClick for this Calendar Object.

    THanks,
    -John

  2. #2
    Chris Alton Guest

    Re: Run time event handelers


    If you want to add a Handler at run time all you have to do is add this line
    AddHandler newObject.MouseDown(or whatever event you want it to fire against),
    AddressOf buttonObject_MouseDown (This is the name of the Public Sub that
    you want to handle the event)

    So in your case it would look something like this.

    AddHandler cmd.DoubleClick, AddressOf calendarHandler_DoubleClick

    Public Sub calendarHandler_DoubleClick(ByVal sender as Object, e as System.Windows.Forms.MouseEventArgs)
    (Don't know if these parameters are correct so don't quote me on this.)
    ...code...
    End Sub

    Notice you don't need to add a Handles keyword at the end of the sub declaration
    since the AddHandler takes care of that for you every time you create the
    calendar object. Just put the AddHandler in before you add the control to
    the controls list and that should take care of it.

    Good luck,

    Chris

    "John" <knowthediff@hotmail.com> wrote:
    >
    >Can Event handlers be built to handle Objects that are created at runtime?
    >
    >Lets say I want the System.Windows.Forms.MonthCalendar() to be created and
    >displayed at the click of a button. I can do this with out a problem.

    Here's
    >the code:
    > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

    System.EventArgs)
    >Handles Button1.Click
    >Public cmd As New System.Windows.Forms.MonthCalendar()
    > cmd = New System.Windows.Forms.MonthCalendar()
    > cmd.TodayDate = "10/8/2001"
    > cmd.Location = New Point(sender.location.x + sender.width, sender.location.y
    >+ sender.height)
    > cmd.Name = "calPopUp"
    > Controls.Add(cmd)
    > cmd.BringToFront()
    > End Sub
    >
    >Now I want an even handler to handle the doubleclick events for the calendar.
    > Can this be done?
    >I do not want to create a generic event handler that handles all DoubleClick
    >Events just the DoubleClick for this Calendar Object.
    >
    >THanks,
    >-John



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links