|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Contents
IntroductionIn this article, you will get a walkthrough of creating an Application Event Handler Component (AEHC) for any WinForms application in .NET. AEH is an event handler for your WinForms application. The primary functionality of this component is to raise an event after every form loads into memory so that we can handle its event for performing any custom code processing and return the control to the form after processing. Let’s consider a scenario. While implementing security into our application, we may have to disable or enable certain controls on every form according to the role of the user accessing the form. For this, we will call a custom method [for instance, Now, with AEHC, we only call the method What is Application Event Handler Component?In ASP.NET, we can create custom HTTP Modules for adding custom processing to each and every web page in web applications at a global level. One of the benefits you get using a HTTP Module is that you do not need to call them from each and every web page; they will automatically get executed for each of them. In Application Event Handler, you will achieve a similar functionality of HTTP Modules. However, AEHC will be specific to every application in contrast with HTTP Modules which can be used machine wide. Application Event Handler Component keeps track of the forms being loaded and unloaded throughout the application’s lifecycle and generates events like
Figure 1 Application Event Handler Component Lifecycle Dim WithEvents myHandler As New AEHC.ApplicationEventHandler
Public frCol As ArrayList
Public Sub main()
'Attaches the Form Filter to the Application
myHandler.Init()
Application.Run(New Form1)
End Sub
Private Sub myHandler_FormLoaded(ByRef sender As System.Windows.Forms.Form, _
ByVal e As AEHC.AppEventHandlerArgs) Handles myHandler.FormLoaded
sender.Text = Now.ToLocalTime
End Sub
Private Sub myHandler_FormUnloaded(ByVal e As AEHC.AppEventHandlerArgs) _
Handles myHandler.FormUnloaded
MsgBox(e.FormsCollection.Count & " in memory")
End Sub
Architecture of Application Event Handler ComponentApplication Event Handler component is built upon window messages released by WinForms. The core part of AEHC is a Let’s get into the details and see how Application Event Handler component works (see Figure 2). When you load a WinForms application, it generates messages for most of its events. You can filter the message queue by using a custom message filter. Let’s create a Forms Collection to hold all the forms which are being loaded throughout the application. Get the Active Form in the application. Check if the form exists in the Forms Collection, add the Form in the collection if not. Now add a handler to
Figure 2 Workflow of Application Event Handler Component Creating an Application Event Handler ComponentApplication Event Handler is composed of two classes:
Using Application Event Handler ComponentNow all said and done, you have finished creating the Application Event Handler Component. Let’s learn to use the same, in your WinForms project. Create a new WinForms application in VB.NET. To use AEHC with VB.NET WinForms application, you will need to add a module and code a Add a reference to AEHC, and instantiate in the module using Initialize the instance by calling In the Dim WithEvents myHandler As New AEHC.ApplicationEventHandler
Public Sub main()
‘Initialize the component
myHandler.Init()
Application.Run(New Form1)
End Sub
Private Sub myHandler_FormLoaded(ByRef sender As _
System.Windows.Forms.Form) Handles myHandler.FormLoaded
Dim label1 As System.Windows.Forms.Label
label1 = New System.Windows.Forms.Label
sender.SuspendLayout()
'Initialize the Label
label1.Font = New System.Drawing.Font("Arial", 14.25!, _
System.Drawing.FontStyle.Bold, _
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
label1.Location = New System.Drawing.Point(8, 8)
label1.Name = "Label1"
label1.Size = New System.Drawing.Size(208, 23)
label1.TabIndex = 0
label1.Text = " Application Event Handler Component Magic "
'
sender.Controls.Add(label1)
sender.ResumeLayout(False)
End Sub
Private Sub myHandler_FormUnloaded() Handles myHandler.FormUnloaded
MsgBox(myHandler.FormsCollection.Count)
End Sub
Run your project to see the magic of your Application Event Handler Component. You will see a label with text “Application Event Handler Component Magic” on your form. To do more testing, you can also place a button on the form and create new instances of the form; you will get amazing functionality like all the forms being displayed with the label control. ConclusionApplication Event Handler Component is remarkably very powerful for WinForms developers, which they can extensively use in UI Customization of WinForms, along with other tasks. They can make use of the Message Filters are one of the easiest ways to add custom processing for each Form in your WinForms application. In fact, you can imitate some more good features of ASP.NET in your WinForms apps by using the same mechanism. In future columns, I'll explore more on implementing ASP.NET's features in WinForms, like Caching, etc.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||