Results 1 to 5 of 5

Thread: WithEvents

  1. #1

    Thread Starter
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    WithEvents

    Dear All,
    What is the use of with Events in VB

    Dana
    Please mark you thread resolved using the Thread Tools as shown

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: WithEvents

    It means that, the Object you are declaring with the WithEvents statement, has some Events and you want to work with those Event Procedures.

    When you doubleclick a control, VB autometically writes the event procedures. But, for objects declared with WithEvents, you have to write the event procedures yourself.

    Take a look at the following example written by Static.
    VB Code:
    1. Dim WithEvents HTML As HTMLDocument
    2.  
    3. Private Sub Form_Load()
    4.     WebBrowser1.Navigate "http://www.google.com"
    5. End Sub
    6.  
    7. Private Function HTML_ondragstart() As Boolean
    8.     HTML_ondragstart = False
    9. End Function
    10.  
    11.  
    12. Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    13.     If (pDisp Is WebBrowser1.Application) Then
    14.         Set HTML = WebBrowser1.Document
    15.     End If
    16. End Sub
    In the above example, HTML is an object and it has some events. But as it is not a control (VB will not write it autometically), and we don't have the source code.
    So to trap the dragging Static has written the HTML_ondragstart event procedure manually.
    So, whenever user drags a link, the drag event fires, and the code inside the event procedure will run.

    More more reference see:
    http://www.developer.com/net/vb/article.php/1541411
    http://www.codeguru.com/columns/vb/article.php/c6557/
    and your MSDN CDs.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: WithEvents

    Typically I will use With Events if I'm dynamically creating controls so that I can code the controls event (usually the click event) once the control has been made.

  4. #4
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: WithEvents

    @Hack,
    Isn't it easier to use a control array and add the first control at designtime (Index=0) and rest of the controls at runtime ?
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: WithEvents

    Quote Originally Posted by iPrank
    @Hack,
    Isn't it easier to use a control array and add the first control at designtime (Index=0) and rest of the controls at runtime ?
    Yes...but not nearly as much fun!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width