VB.NET Tutorial - Create a DLL / Class Library (Visual Basic .NET)

VB.NET Tutorial - Create a DLL / Class Library (Visual Basic .NET)


In this Visual Basic .NET tutorial I will be showing you how to create and use your own DLL (Dynamic Link Library) in your VB.NET projects.

We will create a DLL file for managing form graphics and use that DLL to draw images to forms and form controls.

TUTORIAL FEATURES:
DLL - Library, Namespace, Class
Graphics - System.Drawing, Image
Form Controls - Button, PictureBox
Class - Image Manager
Closed Caption:

welcome to the vb toolbox in this
tutorial i will be showing you how to
create dll files of your own as well as
how to reference them and use them in
future projects had dll files are
dynamic link libraries which are
essentially a reusable collection of
function subs and various other
resources
let's go ahead and get started by
creating a new project and this first
one will be of windows forms application
this is going to be our test environment
to test the functionality of our dll so
I'm just going to call this dll project
and hit ok that will generate a new form
template for us and I think what we're
going to do is create a graphics library
that we can reference and pull images
into our forum and draw on the form with
so the next thing that we will do is
actually create our dll project are
actual dll and to do this we're going to
add another project to our solution
explorer
so I'm going to go to file add new
project
whoops we have to save the first one
before we can add it so go ahead and
save it and from here
what will select is a class library so
because it gave me a graphics library
and it just call it
gfx and hit OK
and call it a GF x lib or something like
that you want so that generates the the
dll template for us here and it's ready
to go it's very basic and simple
so a dll consists of a namespace similar
to when you do in imports
these are namespaces and you know if you
import the system's namespace and then
there are also some namespaces under
that like . net
you'll see in the curly braces there so
that's exactly what we're creating and
that creates sort of a hierarchy or tree
of available sub classes and objects and
functions that we create so i'm going to
create a namespace and name it
GDI so i'm going to say
namespace GDI because we're going to be
using this for graphics library or
referencing gdi+ of the graphics device
interface now what we want to do is
build our classes inside of this
namespace so they're accessible and we
reference it so i'm going to go ahead
and this copy or cut this out of here
and place my class design in here and
the class i'm going to take the default
here and rename it to
image manager something that's easy to
identify want to make sure we keep the
vb extension on the end of that change
this class 12 image manager and so this
should already be you know if we
compiled this and created the dll and
brought it into our RDL project we
should actually already be able to do an
import we would do imports GFX . GI
so now we can begin adding some
information to our class some functions
or subs but before we do that we're
going to first have to create a
reference to the system . drawing
namespace this is normally accessible in
windows forms application but when you
create a dll it isn't accessible by
default so usually can do imports system
. drawing but as you see it doesn't
exist yet because it has no reference so
first we have to go to project and add
reference and then we'll go to our
dotnet and find system . drawing and
here it is and i'll select ok after
selecting that and it will create a
reference and now we should be able to
actually bring it in so say
system.drawing
net that gives us access to graphics
objects and drawing routines and other
GI function so now we're going to create
a dictionary for to store images in if
you're not familiar with the dictionary
collection it's very similar to a list
except that its key indexed
it requires a unique key for each entry
in it
so let's add a public images as new
dictionary of and for the key
we're going to store a string value so
we can reference each item or image in
this dictionary by its name
and finally the image and image is a
system that drawing type i think yes
system system not drawing . bitmap so
this will store yet the actual image and
all of its components for each record in
this dictionary
now we're going to need a way to
actually load these images
I'm not going to be adding in for this
tutorial a windows open file dialog
which would actually be really handy i
think but for the sake of time and then
it can keep this simple and just copy my
images into the debug folder with the
project for fast referencing
them you know just relative passing so
I'm gonna do a public sub low damage and
I'm gonna give it a couple of parameters
here and this load image set will be
accessed from our external application
whatever application or drawing from or
referencing this dll from so I'm gonna
do the first parameter is going to be a
name and that will reference this string
index in our dictionary so i'm going to
do name and string and then the file
path as string and what I'm going to do
is say if images referencing our
dictionary that contains key and the
name of the key then exit sub
because if the name already exists in
the dictionary of the key already exists
it can't be added twice
these have to be unique so if if it if
the image is already in there then we
want to abort the load so that's what
we're doing here
otherwise it's a girl's try
dim i as image equals image . from file
and then we'll load the image from the
flower path that is specified and
finally we will add the new image to our
dictionary so we'll see images . ad and
we have to give it a key value
so the key value will be the new name
that specified and the image that was
discovered on the path as simple as that
and we're using a try-catch statement
here just in case there's any errors
retrieving the image of the file path is
not found or the images of a an
incorrect format we want to catch those
exceptions and not crash your
application so mega throw any exception
messages back if that happens just let
it continue on gracefully
alright so the next sub in our class
let's go ahead and add a remove image
subsalicylate public so remove image and
we need to specify the name of the image
key as a string and here we'll just say
if images . contains key name meaning
that the name index was found in this
dictionary if that's true and the image
exists then i'll say images . remove and
will specify the name of the key piece
kick
and let's see let's add now we need to
get the important part here and that's
how to draw the image back to our
application or form or whatever the
project were loading it into now because
our dll is sort of an external feature
to our applications that we bring it
into it doesn't know the names of the
objects in the project so we need to
kind of make it
agnostic or make it so it will work with
pretty much any application by creating
references to objects or sort of vague
references to those objects are going to
say public sub drawimage and to create a
reference to the object we're going or
the object surface that we want to draw
and i'm going to add surface as a
parameter and that's just going to be an
object so this could be either
our main forum in our application or any
of the other features like a picture box
or something like that that has a
graphics object that we can draw two so
now we need to reference the image that
we want to draw by its name or key in
our dictionary so same name as string
and also we want a position to draw two
on the screen or the control that we
want to draw on so we're going to say
position as a point that gives us an XY
coordinate on our screen that we can
draw two and finally I'm going to add an
option
an optional reference for this size that
we want to draw - maybe want to draw
draw at full size in which case you
might not specify anything otherwise
you might want to control the size or
scale it down to fit in a certain area
so the size would be more relevant there
so what we're going to do is create an
optional value where to stay optional
sighs as a point
so the X Y value and we're going to set
a default value here you have to give it
a default value in case the user does
not actually set the size
mhm and we're just going to set that to
nothing so we just passed a null value
through there
ok now that we have our sub created
there's a few things we need to do here
first is going to be
let's go ahead and throw in a try-catch
here because if there's an error or if
it is unable to draw the image for some
reason or the image doesn't possess a
graphics object in your or the object
doesn't possess a graphics object then
we'll have nowhere to draw a little
throwback in there so we want to again
catch those gonna say if images .
contains key make sure that the image
exists that would also throw an error if
we look I tried to draw an image that
didn't exist if it exists then go ahead
and draw so we'll say tim g as graphics
equals surface
that's our whatever the former form
object that we want to draw on . now it
you want because we're using sort of a
vague object to relay that you know the
surface that we want to draw - we're not
going to see its actual
please sorry about that we're not going
to actually see its properties
so there is in our forum original form
if we go back here you'll see that it
has a create graphics function
ok so this creates a reference to the
objects actual graphics and that's what
we want to call here so we won't see it
but it is there will say create graphics
and it should accept it and then i'll
say if sighs . is empty then we'll just
draw it at full size to whatever
position we specify social space
gee . drawimage and we have to select
the image that we want to draw so we're
going to say images
that's our dictionary and reference its
key
so the key that we want to draw is the
name that we choose
and then we have to draw the position
and will select whatever position we
specified in our parameter so that's
pretty simple if it's not empty meeting
the user-specified a size will say gee
drawimage
images name and this time we're going to
instead of specifying a point or just a
spot an XY coordinate on the screen to
draw - we're going to draw a new
rectangle which has the XY position as
well as an ex-wife or size so i'll say
new rectangle and position . x
coordinate for the location and position
. y coordinate and now we need to
specify the width and height and we'll
grab those from the size parameter will
do the exact same thing with the size .
x and size . why
all right I still have a get rid of that
here real quick
and I think that's really all we need
here in this particular namespace now
you could if you wanted to create a more
organized tree or just added if
additional features you can add a number
of namespaces to your dll you can also
nest those you can create another one
here if you want it inside of the GI but
for this example we won't do that
so our dll once we build this should be
ready to use i'm going to go ahead and
save the projects and going to go ahead
and build this dll by right clicking my
solution explorer and selecting build on
my gfx and now if we right click on that
and go to open folder in windows
explorer
we should be able to go into the bin in
debug and there's our dll right there
it's ready to be used
so now that we have a dll t use now we
can begin placing that and implementing
it in any other windows forms
application which in this case is going
to be our dll project so to do this
we're going to have to reference it much
like we did the system drawing here we
go back to our main project if you click
the project header in your solution
explorer and then go to project and add
reference and go to browse or projects
actually think it will actually
automatically create a reference since
we created these in the same mix same
solution and there is our dl
so we can use that or if you can't find
it in here just browser drive for your
folder that the dll compiled into and
once you add that reference
we should be able to go into our forum
and important say imports
there it is gfx namespace GI and there
you have it
so now that we've done that we should
have access to any of the functions that
were available in here or any of the
subs and variables that we made public
so let's go ahead and try adding one of
those i'm going to go ahead and add a
reference to our image manager class
this guy that we created here by
creating a new instance in my form
what I'm going to do is say just do it
private image man as new image manager
there it is see that image manager would
have would not have shown up in this
list if we had not imported our deal out
so now what I need to do is actually
have some images to load so I'm gonna
grab some and place them because i'm
going to use relative passing instead of
a an open file dialog
I'm just going to put place the images
directly and the debug folder with my
project output so i'm going to right
click in my solution Explorer on my dll
project and go to open folder in windows
explorer been debug and i will place
them in here with my executable so give
me just a moment here to find my
image files
and get a copy of them right here so I'm
just going to copy
it's both . jpg in here as well as maybe
the toolbox image
no I should be able to use those
relative passing so with those available
should be able to load these images into
my image manager so I'm gonna in my form
load event i'm going to add image man .
load image and i'm going to give it a
name for the key
the dictionary key i'm just going to
call this one ve toolbox and now i have
to supply a path of file path which you
could pipe in again from an open file
dialog if you wanted if you're browsing
around your machine
that's pretty handy but in this case
it's bright with the exe file so i can
just reference its file name
so maybe toolbox . PNG and it accepted
those parameters
so I'm going to do one more load the
other image
this one's a JPEG so I'm going to just
bolt for the name and both the jpg for
the path
now when I run the app
these will be loaded into my in an image
manager coming from my dll i'm going to
want a couple other form controls here
is well so you can add a couple of
buttons and a picture box
actually before we do that we could try
just drawing an image to this to the
form i will get will get there
ok let's go ahead and just add those
buttons just going to leave the default
names on those button one button to but
i'll call this one is change the text
caption to draw
copy that paste the copy of that and
this one call
delete
and let's grab a picturebox stretch this
out just a little bit
oops
okay i'm gonna throw a border on that so
i can see it
borderstyle fix single
that works pretty good all right now
with those
let's go ahead and and our first drawing
has a background image for the forms we
don't click on my farm going to my form
load event here so we can access our
other form events at you don't want to
try drying usually directly from the
form load because if the form has a
completed its initialization anything
that you try to draw will not be drawn
until after the form is completely
initialized so we're going to use the
form paint event
this fires up after the form has already
loaded
so any drawing routines that you do
could be placed here to immediately draw
so inform one . paint let's call our
image managers drawimage routine and C
word with that draw image and so the
surface that we want to draw two is
going to be the main form form one
referencing form one from in itself also
we have to use as me so that pet that
passes off the form to the dll and the
dll reference its create graphics from
there
so next up we have to name the image
that we want
the image key then i'm going to use for
my background image is going to be bolt
so I'm going to grab that as the
background and next up we have need a
position to draw too so i'm going to
create a new . and I'm sorry about that
i need to specify play kept place that i
want to draw it and in this case I want
it to be drawn at the very top left most
pixels so 00 is well start drawing from
0 x is 0 Y that is so that's going to be
my position and let's see
for the size I want to specify size here
I don't want to just draw to its natural
size want to want it to conform to our
main forum perfectly so I want it to be
scaled with the form so I'm going to
specify another . and this will be for
the height and width and i'm going to
reference the forms heightened when I
say me that with for the XOR for the
width and me . height to the items and
that will generate a rectangle based on
the top and left coordinate as well as
the width and height
so
if we go ahead and run that and we'll
see if our dll is passing information
properly and look at that you can see
the background image will see if its
scales with the form perfect
that's awesome it's a picture of
lightning and took in my backyard last
summer
so if you're wondering
so now we want to be able to draw an
image into this box here into the
picture box
there's a couple ways we could do it we
could actually set the image property of
the in the picture box to be equal to
the image or we can scale it in just
like we scale this to the form which is
what we'll do
and when we scale it and instead of
actually using the image property of
this will actually get me drawing onto
the surface of the the picture box
instead of setting its image property so
we go back to form one and double click
on draw it will create a click event for
our button and here we can call our
image manager
drawimage again this time i will be not
referencing the main formats that will
be referencing the picturebox a
picturebox one will be our drawing
surface and for the name
I want to draw the vb toolbox image so
I'm going to use that referencing it
from the dictionary by its name
and for the position i wanted to draw to
the top left most corner of our picture
box again so i'm going to do a new point
zero zero like we did before
and keep in mind again this is 0 0 of
the picture box because this is now our
graphics objects are drawing surface so
00 is right here this time instead of
over here and for the size
we're going to do another . so i do new
. and this time I'll reference picture
box . with and picturebox one . right
right
close that off and it should be ready to
go
now let's give that a shot
yeah you look at that it's working
beautifully
so r dl l is working quite nicely
now what if we wanted to delete
something from our library or dictionary
well currently its storing two images at
runtime
so i am going to use button to and
create a click event for that by
double-clicking on it and say image
manager . remove image referencing our
class again here and I want to remove
the BB toolbox age
now if I don't refresh this what's going
to happen is if i go to delete the image
and you know that's been drawn on there
it has already been drawn to the surface
of this object and no longer requires
the file to even exist because it's not
once it's been drawn here it's part of
that picture box it's not actually an
object anymore so we could delete it and
that removed it from the dictionary
however it did not clear the screen
afterward because this is just drawing
on the picturebox not an image in the
library anymore so what we want to do
after we delete it is go to picture box
1 and refresh it so that'll wipe out
like an etch-a-sketch it's gonna clear
off that drawing surface after we're
done with a so now that we know there
are dll is working properly
it's ready to use and you can put it to
any other applications so you can really
there's a number of things you can do
with these DLLs but it makes it so you
don't have to recode a lot of things
when you create a new application
it seems like there's a lot of tedious
tasks and stuff that are very repetitive
between applications really common tasks
and DLLs can help alleviate that pain by
creating a single library that you can
reference from any other applications so
they are very handy and
well worth using so I hope this has been
helpful tutorial for you give me a
thumbs up if you liked it and share it
with anyone else that you think would
enjoy it
I appreciate you coming along have a
good one right

Video Length: 32:29
Uploaded By: VB Toolbox
View Count: 29,826

Related Software Products
ClassLibrary
ClassLibrary

Published By:
Online TV

Description:
ClassLibrary is the ultimate code library for developers, teams, and enterprises. It provides the best collection of ready-to-use code snippets, classes, how-to`s, and articles. It also allows you to manage your own code and share knowledge within a team by using the fully searchable code repository. ClassLibrary provides both a full-featured standalone Code Explorer,is the Ultimate Code Library for Programmers. Get a jumpstart into programming with thousands of lines of C# and Visual Basic ...


Related Videos
Bollywood Class Library Flash Mob
Bollywood Class Library Flash Mob

Northwestern's Bollywood "Cultural Forms" class puts on a study break show for students studying in the university library.
Video Length: 04:09
Uploaded By: NUBollywoodDance
View Count: 102,127

Insert,Update,Delete,Select using Three Tier demo using ClassLibrary
Insert,Update,Delete,Select using Three Tier demo using ClassLibrary

Three Tier Architecture Demo for INSERT UPDATE DELETE SELECT
Video Length: 31:33
Uploaded By: Hitesh Rathod
View Count: 33,143

Tutorial - Visual Basic 2010 - How to make a dll/Class Library - L3GiT
Tutorial - Visual Basic 2010 - How to make a dll/Class Library - L3GiT

This video is a tutorial on how to make a .dll also known as an application extension ALSO known as a Class Library in Visual Basic 2010 (.NET 4.0). If you are in need to know how to make a Class Library you probably already decent at programming, but if your not that is still okay as I went over some key features such as Sub and Subs being Private or Public as well as Functions being Private, Public and Public Shared. Private Sub or Private Function: If you see the word ...
Video Length: 10:16
Uploaded By: CmptrPrgmr
View Count: 20,873

Creating C# .NET Class Library Assembly Versions and Installing in the GAC Part 1 of 3
Creating C# .NET Class Library Assembly Versions and Installing in the GAC Part 1 of 3

Three part video on how to create .NET class library assemblies and handle multiple versions of the code. Also, how to install them into the Global Assembly Cache (GAC) and again, deal with multiple versions. Doug Rees from Black Bear IT is the presenter. hr / bClosed Caption:/b I there my name's degrees I'm gonna be talking to today about dotnet assembly versioning so just a quick overview all summer the issues that we face on working ...
Video Length: 08:48
Uploaded By: BlackBearIT
View Count: 19,605

A Simple Tutorial on using a Class Library and C# application in one Solution.
A Simple Tutorial on using a Class Library and C# application in one Solution.

A Simple tutorial on creating a class library and implementing a class in a C# application in one solution thereof. Just something to get you started no unnecessary detail. hr / bClosed Caption:/b i'm just going to show you guys how to create a cross librarian and basic example on your current track and we'll just use a class library as sort of a container told all our classes and created the aisle from that and with the ll be able ...
Video Length: 10:01
Uploaded By: Mixo Mushwana
View Count: 19,161

Visual Basic 2008 Tutorial: Class Library / DLL
Visual Basic 2008 Tutorial: Class Library / DLL

This video basically teaches you how to write a class library and deploy it in another application. This is my last video for the year of 2009. Lets say hello to 2010. Please comment, rate and subscribe. Please watch in high quality.
Video Length: 05:15
Uploaded By: monsterhunter445
View Count: 15,348

Using Entity Framework within a Class Library DLL
Using Entity Framework within a Class Library DLL

This short video walks you through how to use the Entity Framework inside of a Class Library DLL so that you can reuse the Library in multiple projects. Source Code: http://www.digioz.com/downloads/tutor... Northwind Database: http://northwinddatabase.codeplex.com/ Website: http://www.digioz.com hr / bClosed Caption:/b in this videofont color="#CCCCCC" I will walk you through/font how to use the entity framework from within a library ...
Video Length: 10:29
Uploaded By: DigiOz Multimedia
View Count: 13,041

Angell EYE PHP Class Library for PayPal Overview
Angell EYE PHP Class Library for PayPal Overview

This is a quick overview of how to use the Angell EYE PHP class library for PayPal to process payments using Payments Pro, Adaptive Payments, or PayFlow Pro. hr / bClosed Caption:/b high winds and rain Jules angel I swim do a quick overview the using this PHP library for Pay Pal their bill makes it pretty quick and easy to integrate a pulse web services and a pretty much me PHP the project might be working on a pretty ...
Video Length: 07:21
Uploaded By: Andrew Angell
View Count: 12,992

Visual Basic Tutorial - 179 - FTP Downloader Part 2   Referencing A Class Library
Visual Basic Tutorial - 179 - FTP Downloader Part 2 Referencing A Class Library

Facebook - https://www.facebook.com/TheNewBoston... GitHub - https://github.com/buckyroberts Google+ - https://plus.google.com/+BuckyRoberts LinkedIn - https://www.linkedin.com/in/buckyroberts reddit - https://www.reddit.com/r/thenewboston/ Support - https://www.patreon.com/thenewboston thenewboston - https://thenewboston.com/ Twitter - https://twitter.com/bucky_roberts hr / bClosed Caption:/b hello youtube same here from YouTube dotbr ...
Video Length: 05:11
Uploaded By: thenewboston
View Count: 10,116

Copyright © 2025, Ivertech. All rights reserved.