Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Adding events dynamically in a Windows Form

Total Hit ( 2950)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


The new AddHandler statement makes it possible to attach event dynamically, that is without having to bind them via static code based on the Handles keyword. This keyword is also useful to have all the controls on a form share the same event procedure.

For example, say that you want to change the background of all textbox controls on the form when the focus enters in them, and restore the original color when the focus leaves them. Here's the code code that does the trick:

Click here to copy the following block
Sub SetupEventHandlers()
  Dim ctrl As Control
  For Each ctrl In Me.Controls
    If TypeOf ctrl Is TextBox Then
      AddHandler ctrl.GotFocus, AddressOf Controls_GotFocus
      AddHandler ctrl.LostFocus, AddressOf Controls_LostFocus
    End If
  Next
End Sub

Private Sub Controls_GotFocus(ByVal sender As Object, _
  ByVal e As System.EventArgs)
  Dim ctrl As Control = CType(sender, Control)
  ' In a real application you should use system-defined colors.
  ctrl.BackColor = Color.Yellow
End Sub

Private Sub Controls_LostFocus(ByVal sender As Object, _
  ByVal e As System.EventArgs)
  Dim ctrl As Control = CType(sender, Control)
  ' In a real application you should use system-defined colors.
  ctrl.BackColor = Color.White
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.