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.
Open Visual Studio .NET
Start a "New" VB Windows Application project
Project name does not matter
Once open, add a standard ListBox control to Form1
In the Solution Explorer, notice the icon () being shown for Form1.vb. In a
moment you will see the icon change.
Add the following items to the Collection property of ListBox1
Apple
Banana
Berry
Fig
Grape
Melon
Orange
Peach
Pear
Open the code view for Form1
Expand the Form1 region "Windows Form Designer generated code"
Preform a Search-and-Replace
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
Close the Search-n-Replace window
In the "Windows Form Designer generated code" region, locate the following
line:
FriendWithEvents MyListBox1 As System.Windows.Forms.MyListBox
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.
Alter the line to read:
FriendWithEvents MyListBox1 As MyListBox
Note: MyListBox will still appear as error with squiggly underline
because the assembly containing the declaration has not yet been compiled.
A few lines further down, locate the line:
Me.MyListBox1 = New System.Windows.Forms.MyListBox()
Alter the line to read:
Me.MyListBox1 = New MyListBox()
Note: MyListBox will still appear as error with squiggly underline
because the assembly containing the declaration has not yet been compiled.
Open the Designer view and notice your ListBox control has vanished.
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)
PublicClass MyListBox
Inherits System.Windows.Forms.ListBox
PublicShadowsEvent Click(ByVal sender As Object, ByVal e As EventArgs, _
ByVal i AsInteger)
PublicShadowsEvent Resize(ByVal sender As Object, ByVal e As EventArgs, _
ByVal i AsInteger)
PrivateWithEvents mListBox As System.Windows.Forms.ListBox
PublicSubNew()
MyBase.New()
mListBox = MeEndSubPrivateSub mListBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles mListBox.Click
MsgBox("ListBox Clicked")
RaiseEvent Click(sender, e, 1)
EndSubPrivateSub mListBox_Resize(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles mListBox.Resize
RaiseEvent Resize(sender, e, 1)
EndSubEndClass
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.
Rebuild the solution.
Notice the Solution Explorer Icon () for Form1.vb has changed.
Notice the list box still does not show in the design window.
Close the Design window and re-open it.
The MyListBox now shows.
The ListBox control has been subclassed.
To test:
Run the application.
Click on any item in the list box
The Click_Event is intercepted by your sub-class and a message box is shown,
then the event is passed up the chain.
You have started with a nice idea, but: a. the toturial does not explain how you do all that stuff, why 'with events'? why an object holding the classes instance? what is shadowing? etc.
b. you don't need the additional instance holder, you can use: sub myHandleMethod (arguments) Handles MyBase.EventName
please remember if the person reading this knew all that stuff he wouldn't be reading this... think about it.