Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (43)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : .NET : VB.NET : Gate to Delegates in VB.NET
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

Now lets create a Windows form which interfaces with the Printer class. The Windows form has a Status panel which displays the Ink status of the cartridge(Full, Half, Low and Empty) and a Trackbar. We can decrease/increase the level of ink in the cartridge using the Trackbar.

  ' ** Create an instance of the Printer class    
  Dim oPrinter As New Printer

  Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    ' ** register the Delegate of CartridgeStatus Method with the Printer class 
    ' Notice that when we pass the Address of the Method along with a new
    ' instance of the Delegate, New Printer.CartridgeEventHandler       

    oPrinter.SetDlgRef(New Printer.CartridgeEventHandler(AddressOf _
      CartridgeStatus))
    oPrinter.Name = "HP DeskJet 640C"
    ' ** Set the level of ink in the cartridge
    oPrinter.InkLevel = 100

    ' ** set the position of the Trackbar based on the value of the Ink level
    Me.trInkLevel.Value = (oPrinter.InkLevel / 100) * trInkLevel.Maximum
  End Sub

  ' ** The Method that will invoked by the Delegate, Notice the Method signature
  ' is the same as that of the Delegate
  ' ** The status panel will be updated with values based on the values that was
  ' passed by the delegate
  Private Sub CartridgeStatus(ByVal nCartridgeState As Printer.CartridgeState)
    Dim sState As String

    Select Case nCartridgeState
      Case Printer.CartridgeState.FULL
        sState = "FULL"
      Case Printer.CartridgeState.HALF
        sState = "HALF"
      Case Printer.CartridgeState.LOW
        sState = "LOW"
      Case Printer.CartridgeState.EMPTY
        sState = "EMPTY"
    End Select

    pnCartridgeInk.Text = sState
  End Sub

  ' ** As the Trackbar is scrolled, the InkLevel property of the Printer is also
  ' updated
  Private Sub trInkLevel_Scroll(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles trInkLevel.Scroll
    oPrinter.InkLevel = (trInkLevel.Value / trInkLevel.Maximum) * 100
  End Sub

Imagine the number of applications you can have with the power of Delegates woven into your programming! To be crystal clear with the usage of Delegates, I suggest you get your hands dirty and try examples on your own and don't delegate it to somebody!

The above example is that of Singlecast Delegates. Now what if you want the Delegate to represent Multiple Methods ...sounds like fun,thats where Multicast Delegates come in. But that is in the next section later on this week. Ciao!


Previous ( 1 Gone )( No Further Pages )

Related Articles
  1. Professional VB.NET Design Patterns : Between the Tiers: Design Patterns and .NET Remoting
  2. Professional VB.NET, 2nd Edition : Threading

Comments/Questions ( Threads: 1, Comments: 1 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. calling of paint event of a form by clicking button control

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.