Creating a .NET Component with Visual Basic

Introduction

A couple of months ago, I wrote an article about a user control, and I touched a little bit on Components. With this article, I will demonstrate how to make your own usable component.

What Is a Component?

A component will be output as a DLL. If you do not know what a DLL file is, have a read through here. To understand what a component is, I cannot put it better than MSDN, here, from which I quote:

.NET components provide a programmable interface that is accessed by consumer applications (often called client applications). The component interface consists of a number of properties, methods, and events that are exposed by the classes contained within the component. In other words, a component is a compiled set of classes that support the services provided by the component. The classes expose their services through the properties, methods, and events that comprise the component's interface.

The benefit of a component is the fact that it is loaded at run time because it is already pre-compiled. You have to understand that a component is not a full physical program. It is simply a component of a program, hence its name. Many people think that components are just loose classes thzt may not have a user interface. That is untrue. Anything can be in a component. If you look at the Namespaces that the .NET framework provides, they are solely classes that expose properties and methods. But, when you look at the controls inside the toolbox, they are also components. This is the benefit of true Object Oriented Programming. This allows many a component to make up a full program.

Creating Your Own Component

Let's do a project!

Start Visual Studio and create a new Class Library project, as shown in Figure 1.

NET01
Figure 1: Selecting the Class Library

I thought it best to keep it rather simple because this is just an example. The premise of today's project is to create a component that can change the mouse cursor to an animated cursor. Now, I know there are easier ways to set the mouse cursor, but hey, I always go overboard in my articles.... :)

Add the Namespaces we will need for the component:

Imports System.IO
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

This imports Windows Forms capabilities, as well the ability to import external libraries, called APIs. If you haven't heard about APIs yet, have a read through here.

Now, add the APIs:

Private Declare Unicode Function LoadCursorFromFile Lib _
   "user32.dll" Alias "LoadCursorFromFileW" (ByVal filename As String) As IntPtr
   <DllImport("user32", EntryPoint:="SetCursor", CharSet:=CharSet.Auto)> _
   Private Shared Function SetCursor(ByVal hCursor As Integer) As Integer
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
   Public Shared Function LoadCursor( _
   ByVal hInstance As IntPtr, _
   ByVal lpCursorName As Integer) As IntPtr
End Function

These APIs allow you to load and set a Windows mouse cursor. Add the variables we will be using:

Private Shared curCursor As Cursor

Private Shared FileName As String

Here, I have created the cursor we will work with, as well as a FileName object which will represent the name of our animated cursor. We're almost done with the component. All you need now is a way to load the animated cursor from a file, as well as to display it, so add the next to methods:

Public Shared Sub ShowCursor()
   FileName = Path.Combine(Application.StartupPath, "dinosaur.ani")

   curCursor = CreateCursor(FileName)
   Dim lHandle As Integer

   lHandle = LoadCursor(0, curCursor.Handle)

   SetCursor(lHandle)

End Sub

Private Shared Function CreateCursor(ByVal filename As String) As Cursor
   Dim hCursor As IntPtr
   Dim result As Cursor = Nothing

   Try
      hCursor = LoadCursorFromFile(filename)
      If Not IntPtr.Zero.Equals(hCursor) Then
         result = New Cursor(hCursor)
      Else
         'could not create cursor
         Throw New ApplicationException("Could not create cursor from file " & filename)
         End If
      Catch ex As Exception
      'log exception
   End Try

Return result
End Function

CreateCursor reads the file, and converts what was read into a usable cursor. ShowCursor displays the cursor object. You can build this DLL by clicking the Build menu item.

You're halfway finished at this point.

Now, we need a project to use this component with. Add a Windows Forms project to the component project by clicking File, Add Project. Once the Windows Forms project has been created, right-click it inside the Solution Explorer and set it as the Startup Project. Your Solution Explorer should look something like what's shown in Figure 2:

NET02
Figure 2: Solution Explorer

Add a Reference to your component inside the Windows Forms Project by clicking Project Add reference, as shown in Figure 3.

NET03
Figure 3: Project Reference

Add the last piece of code to your Windows Form:

Imports Component_Example_DLL
   Public Class Form1

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         Component_Example_DLL.AnimatedCursor_Ex.ShowCursor()
      End Sub
   End Class

Conclusion

I hope this article has been insightful and that you have enjoyed it. Until next time, cheers!

About the Author

Hannes du Preez is a Microsoft MVP for Visual Basic for the seventh year in a row.



Related Articles

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • Companies must routinely transfer files and share data to run their business, work with partners, and speed operations. However, many find the traditional approach to file transfer lacks necessary security, is too complex and difficult to manage, does not support the levels of automation needed, and breaks down when addressing the file transfer requirements of new areas like Big Data analytics and mobile applications. This QuinStreet SmartSelect discusses how the changing business environment is making the use …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds