Dear All,
What is the use of with Events in VB
Dana
Printable View
Dear All,
What is the use of with Events in VB
Dana
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.
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.VB Code:
Dim WithEvents HTML As HTMLDocument Private Sub Form_Load() WebBrowser1.Navigate "http://www.google.com" End Sub Private Function HTML_ondragstart() As Boolean HTML_ondragstart = False End Function Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant) If (pDisp Is WebBrowser1.Application) Then Set HTML = WebBrowser1.Document End If End Sub
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.
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.
@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! :DQuote:
Originally Posted by iPrank