5,316,870 members and growing! (16,573 online)
Email Password   helpLost your password?
Languages » VB.NET » General     Beginner

Turorial: Subclass the Listbox to handle Events

By FlashMerlot

A Visual Basic .NET tutorial on subclassing the ListBox control and intercepting ListBox events
VB, Windows, .NET 1.0, .NET, Visual Studio, Dev

Posted: 17 Jul 2002
Updated: 17 Jul 2002
Views: 75,781
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
15 votes for this Article.
Popularity: 3.97 Rating: 3.38 out of 5
3 votes, 27.3%
1
1 vote, 9.1%
2
1 vote, 9.1%
3
2 votes, 18.2%
4
4 votes, 36.4%
5

Sample Image

Introduction

This tutorial walks the programmer through creating a sub-classed ListBox control which intercepts Events and performs additional actions before passing the Event along for further processing by the control itself.

  1. Open Visual Studio .NET

  2. Start a "New" VB Windows Application project

  3. Project name does not matter

  4. Once open, add a standard ListBox control to Form1

  5. In the Solution Explorer, notice the icon () being shown for Form1.vb. In a moment you will see the icon change.

  6. Add the following items to the Collection property of ListBox1

    Apple
    Banana
    Berry
    Fig
    Grape
    Melon
    Orange
    Peach
    Pear

  7. Open the code view for Form1

  8. Expand the Form1 region "Windows Form Designer generated code"

  9. Preform a Search-and-Replace

  10. Search for: ListBox
    Replace with: MyListBox
    Match Case: Unchecked-OFF
    Match Whole Word: Unchecked-OFF
    Search Hidden Text: Checked-ON
    Search: Current Document

    Click: Replace ALL



  11. Close the Search-n-Replace window

  12. In the "Windows Form Designer generated code" region, locate the following line:
    Friend WithEvents MyListBox1 As System.Windows.Forms.MyListBox
  13. Notice the line shows an error (with squiggly underline) at the System.Windows.Forms.MyListBox because MyListBox is not a member of System.Windows.Forms object.

  14. Alter the line to read:
    Friend WithEvents MyListBox1 As MyListBox
  15. Note: MyListBox will still appear as error with squiggly underline because the assembly containing the declaration has not yet been compiled.

  16. A few lines further down, locate the line:
    Me.MyListBox1 = New System.Windows.Forms.MyListBox()
  17. Alter the line to read:
    Me.MyListBox1 = New MyListBox()
  18. Note: MyListBox will still appear as error with squiggly underline because the assembly containing the declaration has not yet been compiled.

  19. Open the Designer view and notice your ListBox control has vanished.

  20. Add the new MyListBox Class declaration below (You are adding a new class to the project so be sure to place the new class outside the Public Class Form1 declaration)
    Public Class MyListBox
       Inherits System.Windows.Forms.ListBox
       Public Shadows Event Click(ByVal sender As Object, ByVal e As EventArgs, _
                                  ByVal i As Integer)
       Public Shadows Event Resize(ByVal sender As Object, ByVal e As EventArgs, _
                                   ByVal i As Integer)
       Private WithEvents mListBox As System.Windows.Forms.ListBox
    
       Public Sub New()
          MyBase.New()
          mListBox = Me
       End Sub
    
       Private Sub mListBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
         Handles mListBox.Click
          MsgBox("ListBox Clicked")
          RaiseEvent Click(sender, e, 1)
       End Sub
    
       Private Sub mListBox_Resize(ByVal sender As Object, ByVal e As System.EventArgs) _
         Handles mListBox.Resize
          RaiseEvent Resize(sender, e, 1)
       End Sub
    End Class
  21. Open the Designer View. You will receive two error messages. You are receiving these errors because the assembly containing the declaration has not yet been compiled.

  22. Rebuild the solution.

  23. Notice the Solution Explorer Icon () for Form1.vb has changed.

  24. Notice the list box still does not show in the design window.

  25. Close the Design window and re-open it.

  26. The MyListBox now shows.

  27. The ListBox control has been subclassed.

To test:

  1. Run the application.
  2. Click on any item in the list box
  3. The Click_Event is intercepted by your sub-class and a message box is shown, then the event is passed up the chain.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

FlashMerlot



Occupation: Web Developer
Location: United States United States

Other popular VB.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Subject  Author Date 
GenerallistboxmemberPhan Thanh Lam23:21 20 Sep '06  
Generalhmm....memberFade (Amit BS)9:04 24 Mar '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Jul 2002
Editor: Chris Maunder
Copyright 2002 by FlashMerlot
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project