Handy quick VB functions which I’ve used

Here are some of my most common used functions, maybe you can use them.

How to delete all files from a specific folder

Dim s As String
For Each s In System.IO.Directory.GetFiles("C:\WINDOWS\TEMP")
Next s

Generate a Random number from Min to Max

  Public Function Get_Random_Number(ByVal Max As Integer, ByVal min As Integer) As Integer

        'GetRandomNumber = CInt(Int((vnMaximumNumber - vnMinimumNumber + 1) * Rnd() + vnMinimumNumber))

        Get_Random_Number = CInt(Int((Max - min + 1) * Rnd() + min))
    End Function

Check if a time has passed or not

I use this alot since many times I have the date and time, and sometimes the time, this functions standarize’s it to Short time (example 5:40:00pm) and return True or false if it has passed or not

'---------- Return True is time hasnt arrived yet.
    Public Function IsFuture(ByVal CompareTo As Date) As Boolean
        IsFuture = CDate(Format(Now, "Short Time")) < CDate(CompareTo)
    End Function

'----------------- Return True if time has Passed
    Public Function IsPast(ByVal CompareTo As Date) As Boolean
        IsPast = CDate(Format(Now, "Short Time")) > CDate(CompareTo)
    End Function

Execute External program

This will executre a program and continue, it will not wait.  I use this to send data to my clients but not wait for a answer.

System.Diagnostics.Process.Start(“<program>”, “<Program parameters>”)


System.Diagnostics.Process.Start(“C:\BumpClients.exe”, ” “)

Pause your program

System.Threading.Thread.Sleep(9000) ‘– Time is MS. 9000 is equivalent to 9 seconds

Loading images into Picture Boxes

PictureBox1.Image = Image.FromFile(“c:\images\image.gif”)

A Bare bones Send email program using MAPI/Outlook

Make sure you have Microsoft Outlook object reference and outlook running.

Option Strict Off
Option Explicit On

Imports Microsoft.Office.Interop.Outlook
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        '-------- SET UP EMAIL GOODIES
        Dim objOutlook As Microsoft.Office.Interop.Outlook._Application
        Dim outmail As Microsoft.Office.Interop.Outlook.MailItem

        Dim objNS As Microsoft.Office.Interop.Outlook._NameSpace

        objOutlook = New Microsoft.Office.Interop.Outlook.Application()
        objNS = objOutlook.Session

        outmail = objOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
        outmail.To = "<email address to send to>"
        outmail.Subject = "Here is your subject"

    End Sub

Fade out your program (Main form) upon exit

Private Sub StartupScreen_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
 Dim a As Double
 For a = 100 To 1 Step -10
 Me.Opacity = a * 0.02
Exit Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s