Common Library Functions in Visual Basic


Today, I would like to introduce you, especially the newbies out there, to a few of the most common Library functions in Visual Basic. My aim with this article is to introduce you to the functions, as well as explain with a small example of how the particular functions can be used.

Let’s jump straight in, shall we.


The InStr function returns the position of the first occurrence of one string within another. This means that it will return the character number where the specified search string is present. Here is a small example:

' String to search in.
Dim SearchString As String = "Hannes"
' Search for "e".
Dim SearchChar As String = "e"
Dim TestPos As Integer
' Textual comparison starting at position 1. Returns 5.
TestPos = InStr(1, SearchString, SearchChar)

As you can see, being able to manipulate strings can be quite useful and is a core fundamental in every program. Here are more string functions.


The DateDiff function returns a Long value specifying the number of time intervals between two Date values. This simply means that it gives the difference between two dates. The difference could be in days, months, hours, minutes, weeks, years, and so on. Here is a small example:

Dim Date1 As Date = #28/07/2015#
Dim Date2 As Date = #28/9/2015#
' This will show the number of days between
' 28/07/2015 and 28/09/2015
MessageBox.Show(DateDiff(DateInterval.Day, _
   Date1, Date2))

You will use some sort of Date manipulation daily. Here is a list of more date functions.


You can write run-time messages to the Output window by using the Debug class, which is part of the System.Diagnostics class library. The Debug.Fail method always breaks execution and outputs information in a dialog box. Here is how to use it:

'Some Code
Catch e As Exception
Debug.Fail("Unknown selection " & Selection1)
End Try

Here is more information on Debugging.

For more information on the System.Diagnostics class, read here.


Extracting data from databases is not complicated at all. Any developer needs to know how to do this, as this is essentially what will make your app tick. Here is a small example of extracting information out of a database table:

Dim sqlConnection1 As New SqlConnection("Your _
   Connection String")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader

cmd.CommandText = "SELECT * FROM Table"
cmd.CommandType = CommandType.Text
cmd.Connection = sqlConnection1


reader = cmd.ExecuteReader()
' Data is accessible through your DataReader
' object here.


You can follow this link for more information on the ExecuteReader function.

More information on working with databases can be found here.


Through the years, access to the Clipboard object has become easier and easier. Here is a small example on using the clipboard to copy, cut, and paste information to and from textboxes:

Private Sub btnClipCopy_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnClipCopy.Click
   If txtSource.Text <> "" Then
      If txtSource.SelectionLength > 0 Then
         strSelectedText = txtSource.SelectedText
          ' Put selection on clipboard
         ' Put all text on clipboard
      End If
   End If
End Sub

Private Sub btnClipPaste_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnClipPaste.Click
   Dim ClipText As String   ' Store text from clipboard
   Dim OldText As String    ' Previous text in textbox
   OldText = txtDest.Text   ' Store previous text
   ' Get the clipboard contents
   ClipText = Clipboard.GetText
   ' To paste at back of text; otherwise, just say
   ' textbox2.text = cliptext
   txtDest.Text = OldText & ClipText
End Sub


File.OpenWrite opens an existing file or creates a new file for writing. The OpenWrite method opens a file if one already exists for the file path, or creates a new file if one does not exist. For an existing file, it overwrites the existing characters with the new characters. Here is a small example of its implementation:

   Dim path As String = "c:TextFile.txt"
   ' Open the stream and write to it.
   Using fs As FileStream = File.OpenWrite(path)
      Dim info As Byte() = _
         New UTF8Encoding(True).GetBytes("Testing123")
         ' Add some information to the file.
         fs.Write(info, 0, info.Length)
   End Using

For more information regarding the Using statement, read here.

For more information on the System.IO namespace, have a look through this article.


The FontFamily.GetFamilies method returns an array that contains all the FontFamily objects. In simple terms, it allows you to return a list of all installed font families (names) on your PC. Here is a small example:

Dim ff As FontFamily
For Each ff In System.Drawing.FontFamily.Families

For more information on the Font classes, read this article.


This function Draws a rectangle specified by a Rectangle structure. By knowing this method, you will be able to ease into the other Drawing methods such as DrawEllipse and DrawLine. This should open the door to the world of Graphics programming. Here is a tiny example:

Public Sub DrawSomething(ByVal e As PaintEventArgs)
   ' Create pen.
   Dim BluePen As New Pen(Color.Blue, 3)
   ' Create rectangle.
   Dim rect As New Rectangle(0, 0, 200, 200)
   ' Draw rectangle to screen.
   e.Graphics.DrawRectangle(BluePen, rect)
End Sub

Here is more information on how to draw various shapes and utilize the Graphics methods properly.


Retrieves the specified subkey. Here is a small example:

' Obtain the test key in one step, using the
' CurrentUser registry
   rkTest = Registry.CurrentUser.OpenSubKey _
   Console.WriteLine("Test key: {0}", rkTest)

Here is more information on the Registry classes.


We can use IndexOf to determine if a certain character or phrase is present in the supplied string. A simple example would be to check if an ‘@’ sign is present in an email address. If it is not, we know that the supplied email address is not valid.

Here is a small example:

Dim MainString As String = "ABCDE"
Dim Result As Integer
' result = 3
Result = MainString.IndexOf("D")
MessageBox.Show("D is Character number : _
   " & Result.ToString)

The preceding code segment creates a string object named MainString and holds “ABCDE” inside it. I created an Integer variable to hold the location of the desired character. I then used Indexof to determine where D is inside the string. D in this case, is character 3. A = 0. B =1. C = 2. D = 3.


Although I have highlighted the preceding functions and classes, there are still millions of other functions out there. But, this list will help you get to know all of them better.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read