Creating a custom DLL in Visual Basic .NET
One of the best programming practices is to use DLL (Dynamic Link Library) files to re-use your code and controls across your applications. In this tutorial I will show you how to create a sample DLL file in Visual Basic .NET Professional edition and attach it to a new application.
1)Â Â Â Create a new project, select Windows Forms Control Library and name it MiniCalc
2)Â Â Â You will see that the new UserControl1.vb is added to your project. Right-click on the UserControl1.vb and select Rename from the menu. Name it MiniCalc.vb
3)Â Â Â Go to the Toolbox and add 1 groupbox, 2 textboxes, 4 buttons and 1 label. Align them on the control as on the following image:
Set the following properties:
| TextBox1 | |
| Name | txtValue1 |
| TextBox2 | |
| Name | txtValue2 |
| Button1 | |
| Name | btnPlus |
| Text | + |
| Button2 | |
| Name | btnMinus |
| Text | - |
| Button3 | |
| Name | btnMultiply |
| Text | x |
| Button4 | |
| Name | btnSubtract |
| Text | / |
| Label1 | |
| Name | lblResult |
| GroupBox1 | |
| Text | MiniCalc |
4)Â Â Â Right-click on the form and select View Code. You will see the following lines of code in the editor:
Public Class miniCalc End Class
5)Â Â Â Edit the code to make it as follows:
Public Class miniCalc Private Sub btnPlus_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnPlus.Click lblResult.Text = CInt(txtValue1.Text) + CInt(txtValue2.Text) End Sub Private Sub btnMinus_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnMinus.Click lblResult.Text = CInt(txtValue1.Text) - CInt(txtValue2.Text) End Sub Private Sub btnMultiply_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnMultiply.Click lblResult.Text = CInt(txtValue1.Text) * CInt(txtValue2.Text) End Sub Private Sub btnDivide_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnDivide.Click lblResult.Text = CInt(txtValue1.Text) / CInt(txtValue2.Text) End Sub Private Sub GroupBox1_Enter(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles GroupBox1.Enter lblResult.Text = "" End Sub End Class
6)Â Â Â In the solution explorer right-click on MiniCalc and select Properties:
7)Â Â Â Make sure that the following parameters are set:
8)   Now go to Build→ Build MiniCalc
9)Â Â Â Now, go to the Bin folder in your project folder. In my case, I saved my MiniCalc project under C:\Projects\:
10)   Copy the MiniCalc.dll to any other directory where you want to store your final DLLs. It’s always a good idea to keep your DLL project in case if you will need to modify it in the future. In my case I will copy MiniCalc.dll under C:\My DLLs\ folder.
11)Â Â Â Close MiniCalc project.
12)   Create a new Windows Forms Application project and name it “Mini Calculatorâ€
13)   Go to Project and click on Add Reference…
14)Â Â Â In the Add Reference window click on the Browse tab, locate MiniCalc.dll file and click OK button:
15)Â Â Â In the Toolbox pane right-click somewhere in the empty space and select Add Tab:
16)Â Â Â Name it My Controls and click Enter
17)   Right-click under My Controls and select Choose Items… from the context menus:
18)Â Â Â In the .NET Framework Components tab click Browse.. and locate MiniCalc.dll file.
19)Â Â Â miniCalc.dll will be added to your components collection. Click OK button to close the dialog window.
20)Â Â Â MiniCalc control is now added to My Controls.
21)Â Â Â Drag it to your form:
22)Â Â Â Click F5 to start debugging:
In this tutorial we have learned how to create DLL files and use them across other applications.
Download the sample project here.



















