logo VB.Net - Functions which are hard to Categorise
Guide Contents
DigitalDan Home Page

This page provides examples of functions that do not fit into any of the other categories on this site
 
Is this program already running on a computer?
Public Shared Function Is_Program_Already_Running() As Boolean
Return (Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1)
End Function

 
Replace All Accented Charecters with Similar Non-Accented
Public Shared Function Remove_Accents(txt As String) As String
Dim normalizedString As String = txt.Normalize(System.Text.NormalizationForm.FormD)
Dim c As Char
Dim stringBuilder As New System.Text.StringBuilder
For i = 0 To normalizedString.Length - 1
c = normalizedString(i)
If System.Globalization.CharUnicodeInfo.GetUnicodeCategory(c) <> System.Globalization.UnicodeCategory.NonSpacingMark Then
stringBUilder.Append(c)
End If
Next
Return stringBuilder.ToString()
End Function

 
Test whether a point is inside a polygon
The parameters are Polygon=a set of points defining verticies of a polygon ... target_point=point which may be inside the polygon
The path.IsVisible command can take time to execute when combined with complex polygons. Consider adding code to eliminate any target points outside of a bounding rectangle because this will sometimes reduce program execution times.
 
Private Shared Function Is_Point_In_Polygon(polygon As List(Of PointF), target_point As PointF) As Boolean
' points buildingpolygon - last point does not have to match first - it will always complete the polygon
' pointf appears to be fine (i.e. decimal x and y coordinates allowed (pointF uses single preceision - NOT double)
Dim path As New Drawing2D.GraphicsPath
path.AddPolygon(polygon.ToArray)
Return path.IsVisible(target_point)
End Function

 
Spreadsheet Column Header to Column Number
Function assumes column header A corresponds to column number 0. If you want column header A to correspond to column 1, remove the line column += 1
 
Private Shared Function Column_To_Header(column As Integer) As String
Dim ret As String = String.Empty
Dim modnum As Integer
column += 1
While column > 0
modnum = (column - 1) Mod 26
ret = Chr(65 + modnum) & ret
column = CInt((column - modnum) \ 26)
End While
Return ret
End Function

 
Spreadsheet Column Header to Column Number
Function assumes column header A corresponds to column number 0. If you want column header A to correspond to column 1, change the line Return column - 1 to Return column
 
Private Shared Function Header_to_Column(header As String) As Integer
' strip out any garbage from input
Dim rgx As New Regex("[^a-zA-Z ]")
header = header.ToUpper
header = rgx.Replace(header, "")
If header.Length < 1 Or header.Length > 5 Then
Return -1 ' invalid header
End If
' get column number
Dim column As Integer = 0
header = header.PadLeft(5, "@"c)
Dim tem As Integer
For Each c As Char In header
column *= 26
tem = Asc(c) - 64
column += tem
Next
Return column - 1
End Function

DigitalDan.co.uk ... Hits = 163