The basic procedure to create a library of
Create a new Windows Forms Application. (Applicable to WPF also)
- Under the Projects Properties change the Application Type to Class Library (A DLL).
Note: It's is also a good idea to change the name of the Assembly and Root namespace also. (see why later) - Delete the Form1.
- Add a new UserControl
- Design and Code the Control
- Save the Project.
Example
Let's create a DigitPad Control.
Here's what mine looks like.

Public Class DigitKeyPad
Private Sub Digit0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Digit0.Click, Digit1.Click, Digit2.Click,
Digit3.Click, Digit4.Click, Digit5.Click, Digit6.Click, Digit7.Click, Digit8.Click, Digit9.Click
Dim c As Button = TryCast(sender, Button)
If c IsNot Nothing Then RaiseEvent DigitPressed(New DigitPressedArgs(Integer.Parse(c.Text)))
End Sub
Create an Event which is fired every time a button is pressed.
Public Event DigitPressed(ByVal e As DigitPressedArgs)
The code which enables the value to be passed as a read-only property of the EventArgs.
Public Class DigitPressedArgs
Inherits EventArgs
Protected mValue As Integer = 0
Public ReadOnly Property Digit() As Integer
Get
Return mValue
End Get
End Property
Friend Sub New(ByVal value As Integer)
mValue = value
End Sub
End Class
End Class
Using the Library in a Project
After creating our control library let's using in a separate project.
Create a new Windows Forms Application.
Project -> Add Reference
Under the Browse Tab, locate and select the previously created generated DLL.
Public Class Form1
Dim dp2 As DigitKeyPad
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Programmatically
dp2 = New DigitKeyPad
dp2.Left = 0
dp2.Top = 0
AddHandler dp2.DigitPressed, Sub(ee As DigitKeyPad.DigitPressedArgs)
MessageBox.Show("DigitPad1: " & ee.Digit.ToString)
End Sub
Me.Controls.Add(dp2)
End Sub
End Class
Tools -> Choose Items
Browse -> Select the previously create DLL
Now the control appears in the toolbox.
Public Class Form1
Dim dp2 As DigitKeyPad
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Programmatically
dp2 = New DigitKeyPad
dp2.Left = 0
dp2.Top = 0
AddHandler dp2.DigitPressed, Sub(ee As DigitKeyPad.DigitPressedArgs)
MessageBox.Show("DigitPad1: " & ee.Digit.ToString)
End Sub
Me.Controls.Add(dp2)
' Via Toolbox
AddHandler Me.DigitKeyPad1.DigitPressed, Sub(ee As DigitKeyPad.DigitPressedArgs)
MessageBox.Show("DigitPad2: " & ee.Digit.ToString)
End Sub
End Sub
End Class
Now you are Control Library creating coding ninja






MultiQuote






|