Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / Visual Basic

TeamVision

Rate me:
Please Sign up or sign in to vote.
3.08/5 (11 votes)
16 Nov 2009CPL3 min read 84.6K   5.4K   69  
A simple project task management application. A managed approach to help keep on top of your projects.
Imports System.IO
Imports System.Text.RegularExpressions
Imports vb = Microsoft.VisualBasic

Friend Module modStrings

    Friend Const REGISTRYKEY As String = "Software\Arkitech EBC Corporation\TeamVision\3.0"

    Friend Const HELPRESOURCE As String = "\help\TeamVision.Help.chm"

    Friend ReadOnly Property TemplateDirectory() As String
        Get
            Return ParentDirectory + "\Templates\"
        End Get
    End Property

    Friend ReadOnly Property DataDirectory() As String
        Get
            Return ParentDirectory + "\Data\"
        End Get
    End Property

    Friend ReadOnly Property ParentDirectory() As String
        Get
            Dim dir As New DirectoryInfo(Application.StartupPath)
            Return dir.Parent.FullName
        End Get
    End Property

    Friend ReadOnly Property HelpFile() As String
        Get
            Return ParentDirectory + HELPRESOURCE
        End Get
    End Property

#Region "TrimSpaces"
    Friend Function TrimSpaces(ByRef Text As String) As String
        Try
            Dim Loop1 As Integer
            Dim SpaceCheck As String
            Dim FullString As String = String.Empty

            For Loop1 = 1 To Len(Text)
                SpaceCheck = Mid(Text, Loop1, 1)
                If SpaceCheck <> " " Then FullString += SpaceCheck
            Next Loop1

            TrimSpaces = FullString
        Catch ex As Exception
            Return String.Empty
        End Try
    End Function
#End Region

#Region "TrimChars"
    Public Function TrimChars(ByVal OrigString As String, ByVal CharToTrim As String) As String
        Try
            'Trim chars from the front and end of a 
            '     string
            Dim l As Integer
            l = Len(CharToTrim)
            If Len(OrigString) >= l Then
                Do While Left(OrigString, l) = CharToTrim
                    If Len(OrigString) < l Then
                        Exit Do
                    End If
                    OrigString = Right(OrigString, Len(OrigString) - l)
                Loop
            End If
            If Len(OrigString) >= l Then
                Do While Right(OrigString, l) = CharToTrim
                    If Len(OrigString) < l Then
                        Exit Do
                    End If
                    OrigString = Left(OrigString, Len(OrigString) - l)
                Loop
            End If
            TrimChars = OrigString
        Catch ex As Exception
            Return String.Empty
        End Try
    End Function
#End Region

#Region "Randomizer"
    Friend Function Randomizer(ByVal iStart As Integer, ByVal iEnd As Integer) As Integer
        Dim iRandomValue As Single
        Randomize()
        iRandomValue = iStart + (Rnd() * (iEnd - iStart))
        Return CInt(iRandomValue)
    End Function
#End Region

#Region "FindWords"
    Friend Function FindWords(ByVal SearchIn As String, ByVal SearchValue As String) As Integer
        Try
            Dim regPhrase As Regex
            Dim iCount As Integer

            regPhrase = New System.Text.RegularExpressions.Regex( _
                SearchValue, RegexOptions.Singleline Or RegexOptions.Multiline Or RegexOptions.IgnoreCase)

            For Each m As Match In regPhrase.Matches(SearchIn)
                iCount += 1
            Next m

            Return iCount
        Catch ex As Exception

        End Try
    End Function
#End Region

#Region "CleanString"
    'Function to clean all characters but A-Z, a-z, and 0123456789 
    'When I don't use Regular Expressions I use this function to put 
    'only the characters I want into a string. It is very useful 
    'for password generation. Look up the Dec number on an ASCII 
    'chart and use this for many things. 
    Friend Function CleanString(ByVal strValue As String, _
        Optional ByVal ReplacePeriod As Boolean = False) As String
        Try
            Dim nLength As Integer
            Dim strClean As String = String.Empty

            nLength = Len(strValue)
            For strCounter As Integer = 1 To nLength
                Try
                    Select Case Asc(Mid(strValue, strCounter, 1))
                        Case 65 To 90 'A-Z 
                            strClean &= Mid(strValue, strCounter, 1)
                        Case 97 To 122 'a-z 
                            strClean &= Mid(strValue, strCounter, 1)
                        Case 48 To 57 ' 0123456789 
                            strClean &= Mid(strValue, strCounter, 1)
                        Case Asc(" ") ' Spaces
                            strClean &= UCase(Mid(strValue, strCounter, 1))
                        Case Asc(".")
                            If ReplacePeriod Then strClean &= " "
                            'Case Asc("-")
                            '    strClean &= UCase(Mid(strValue, strCounter, 1))
                        Case Else
                            'All other characters are stripped out 
                    End Select
                Catch ex As Exception

                End Try
            Next strCounter

            Return strClean
        Catch ex As Exception
            Return String.Empty
        End Try
    End Function
#End Region

#Region "Contains"
    Friend Function Contains(ByVal SearchIn As String, ByVal FindValue As String) As Integer
        Try
            Dim regPhrase As Regex
            Dim iCount As Integer = 0

            regPhrase = New System.Text.RegularExpressions.Regex( _
                FindValue, RegexOptions.Singleline Or RegexOptions.Multiline Or RegexOptions.IgnoreCase)

            For Each m As Match In regPhrase.Matches(SearchIn)
                iCount += 1
            Next m

            Return iCount
        Catch ex As Exception

        End Try
    End Function
#End Region

#Region "ReplaceString"
    Friend Function ReplaceString(ByVal strValue As String) As String
        Try
            Dim nLength As Integer
            Dim strClean As String = String.Empty

            nLength = Len(strValue)
            For strCounter As Integer = 1 To nLength
                Try
                    Select Case Asc(Mid(strValue, strCounter, 1))
                        Case 65 To 90 'A-Z 
                            strClean &= Mid(strValue, strCounter, 1)
                        Case 97 To 122 'a-z 
                            strClean &= Mid(strValue, strCounter, 1)
                        Case 48 To 57 ' 0123456789 
                            strClean &= Mid(strValue, strCounter, 1)
                        Case Asc("'")
                            strClean &= Mid(strValue, strCounter, 1)
                        Case Else
                            strClean &= " "
                            'All other characters are replaced with white space 
                    End Select
                Catch ex As Exception

                End Try
            Next strCounter

            Return strClean
        Catch ex As Exception
            Return String.Empty
        End Try
    End Function
#End Region

#Region "ReplacePeriods"
    Private Function ReplacePeriods(ByVal Raw As String) As String
        Try
            Raw = Regex.Replace(Raw, "\.", "  ", RegexOptions.Multiline Or RegexOptions.IgnoreCase)

            Return Raw
        Catch ex As Exception
            Return String.Empty
        End Try
    End Function
#End Region

#Region "TrimSides"
    Friend Function TrimSides(ByVal data As String, ByVal FromLeft As Integer, ByVal FromRight As Integer) As String
        Try
            If Len(data) <= FromLeft + FromRight Then Return String.Empty
            Return Mid(data, FromLeft + 1, Len(data) - FromLeft - FromRight)
        Catch ex As Exception
            Return String.Empty
        End Try
    End Function
#End Region

#Region "String Process Library"
    Public Const CHARSET_LETTERS_UCASE As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Public Const CHARSET_LETTERS_LCASE As String = "abcdefghijklmnopqrstuvwxyz"
    Public Const CHARSET_LETTERS As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    Public Const CHARSET_DIGITS As String = "0123456789"
    Public Const CHARSET_ALPHANUMERIC As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    Public Const CHARSET_PRINTABLE As String = " !""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
    Public Const CHARSET_WHITESPACE As String = vbCr & vbLf & vbTab & " "

    'Pad the left side of the string with enough PadCharacter characters
    'until the string is Size characters long.
    Public Function PadLeft(ByVal Value As String, ByVal Size As Long, Optional ByVal PadCharacter As String = " ") As String
        Dim pad As String = "" & Value
        While Len(pad) < Size
            Return PadCharacter & pad
        End While
        Return pad
    End Function

    'Pad the right side of the string with enough PadCharacter characters
    'until the string is Size characters long.
    Public Function PadRight(ByVal Value As String, ByVal Size As Long, Optional ByVal PadCharacter As String = " ") As String
        Dim pad As String = "" & Value
        While Len(pad) < Size
            Return pad & PadCharacter
        End While
        Return pad
    End Function

    'Are all of the characters in Text letters (i.e., A - Z and a - z)?
    Public Function IsLetters(ByVal Text As String) As Boolean
        Dim i, lLen, nChar As Integer
        lLen = Len(Text)

        For i = 1 To lLen
            nChar = Asc(UCase(Mid(Text, i, 1)))
            If nChar < 65 Or nChar > 90 Then Return False
        Next i

        Return True
    End Function

    'Are all of the characters in Text digits (i.e., 0 - 9)?
    Public Function IsDigits(ByVal Text As String) As Boolean
        Dim i, lLen, nChar As Integer
        lLen = Len(Text)
        For i = 1 To lLen
            nChar = Asc(UCase(Mid(Text, i, 1)))
            If nChar < 48 Or nChar > 57 Then Return False
        Next i
        Return True
    End Function

    'Are all of the characters in Text letters or digits?
    Public Function IsLettersAndDigits(ByVal Text As String) As Boolean
        Dim i, lLen, nChar As Integer
        lLen = Len(Text)
        For i = 1 To lLen
            nChar = Asc(UCase(Mid(Text, i, 1)))
            If (nChar < 65 Or nChar > 90) And (nChar < 48 Or nChar > 57) Then Return False
        Next
        Return True
    End Function

    'Are all of the characters in Text within the specified set?
    'An example of a set is "0123456789 .,Ee-", which should suffice to
    'recognize most simple number formats and even scientific notation.
    Public Function IsInSet(ByVal Text As String, ByVal CharacterSet As String, Optional ByVal CaseSensitive As Boolean = True) As Boolean
        Dim i, lLen As Integer
        lLen = Len(Text)

        For i = 1 To Len(Text)
            If CaseSensitive Then
                If InStr(1, CharacterSet, Mid(Text, i, 1), CompareMethod.Binary) = 0 Then Return False
            Else
                If InStr(1, CharacterSet, Mid(Text, i, 1), CompareMethod.Text) = 0 Then Return False
            End If
        Next
        Return True
    End Function
#End Region

#Region "RecursiveSearch"
    ' This is a function to get all the files in a directory. This will get the files 
    ' in all subdirectories of the parent folder 
    Friend Sub RecursiveSearch(ByRef strDirectory As String, ByRef array As ArrayList, ByVal Pattern As String)
        ' Try to get the files for this directory 
        Dim pFileInfo() As String
        Try
            pFileInfo = Directory.GetFiles(strDirectory, Pattern)
        Catch ex As UnauthorizedAccessException
            MessageBox.Show(ex.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
        ' Add the file infos to the array 
        array.AddRange(pFileInfo)
        ' Try to get the subdirectories of this one 
        Dim pdirInfo() As String
        Try
            pdirInfo = Directory.GetDirectories(strDirectory)
        Catch ex As UnauthorizedAccessException
            MessageBox.Show(ex.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
        ' Iterate through each directory and recurse! 
        For Each dirIter As String In pdirInfo
            RecursiveSearch(dirIter, array, Pattern)
        Next dirIter
    End Sub
#End Region

#Region "VerifyFileName"
    Function VerifyFileName(ByVal Currentname As String) As String
        Try
            If Not IsExist(Currentname) Then Return Currentname
            Dim ext As String = Path.GetExtension(Currentname)
            Dim checkedName As String = Path.GetFileNameWithoutExtension(Currentname)
            Dim i As Integer = 1
            Dim Check As Boolean = True

            Do
                If Not IsExist(checkedName + i.ToString + ext) Then
                    checkedName = checkedName + i.ToString + ext
                    Check = False : Exit Do
                End If
                i += 1
            Loop Until Check = False   ' Exit outer loop immediately.
            Return checkedName
        Catch ex As Exception
            Return String.Empty
        End Try
    End Function

    Private Function IsExist(ByVal filename As String) As Boolean
        If Not File.Exists(filename) Then Return False
        Return True
    End Function
#End Region

End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
Founder Arkitech EBC Corporation
United States United States
MS, BBA, software developer, consultant, and trainer. Specializing in building data-centric applications designed for business, university, community & faith based organizations. Started developing Excel VBA macros and never looked back. Freelance developer utilizing VB.Net, SQL Server, Microsoft Access, and ASP.Net.

Comments and Discussions