Do.Stuff()

No Opinions...just code

clip to send email by opening default client

process.Start("mailto:PJ@Mailman.com?subject=Howdy")

Calculate a fraction of a number

I cant really remember where I got this bit. Takes in a double and returns the
fraction...as in .75 will return 3/4


1
Private Shared Function GetFraction(ByVal d As Double) As String
    2 
    3     Dim Denom As Int32
    4     Dim str As String = d.ToString
    5     Dim Numer As Int32
    6     If str.Contains(".") Then
    7         ' Get the initial denominator: 1 * (10 ^ decimal portion length)
    8         Denom = CInt(1 * (10 ^ str.Split("."c)(1).Length))
    9         Numer = CInt(str.Split("."c)(1))
   10     Else
   11         '{ whole number }
   12         Denom = 1
   13         Numer = d
   14     End If
   15 
   16     ' Use the Euclidean algorithm to find the gcd
   17     Dim a As Int32 = Numer
   18     Dim b As Int32 = Denom
   19     Dim t As Int32 = 0
   20 
   21     ' Euclidean algorithm
   22     While b <> 0
   23         t = b
   24         b = a Mod b
   25         a = t
   26     End While
   27 
   28     If CInt(d) = 0 Then
   29         Return (Numer / a) & "/" & (Denom / a)
   30     Else
   31         Return CInt(d) & " " & (Numer / a) & "/" & (Denom / a)
   32     End If
   33 
   34 
   35 End Function

Connecting To An Excel Spreadsheet as a DB

This will connect top the specified Excel file and treat it as a database.
The SEVERE limitation is that the sheetname must be specified EXACTLY:


1
Dim XLCommand As System.Data.OleDb.OleDbDataAdapter
  2 Dim XLConnection As System.Data.OleDb.OleDbConnection
  3 Dim xDS As DataSet



  1 '{ Create an ODBC Connection to read from Excel spreadsheet }
  2 XLConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " & _
  3    "data source=" & XlFile & "; Extended Properties=""Excel 8.0;IMEX=1;ReadOnly:=True;UpdateLinks:=0""")
  4 XLCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [" & str & "$]", XLConnection)
  5 
  6 xDS = New System.Data.DataSet
  7 XLCommand.Fill(xDS)

Filter DataGridView By Selected Rows

    This DataGridView has a CheckboxColumn named "Keep". If the box for a row
is not selected, it gets added to an arraylist. This arraylist stores the
indexes of the columns that will be hidden


1
Private Sub cmdFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFilter.Click
    2     Dim collist As New ArrayList
    3 
    4     For Each x As DataGridViewRow In myDGV.Rows
    5         If x.Cells("Keep").Value = False Then
    6             collist.Add(CInt(x.Index))
    7         End If
    8     Next
    9 
   10     
   11     FilterColumns(collist)
   12 
   13 End Sub


    1 Private Sub FilterColumns(ByRef arr As ArrayList)
    2     arr.Sort()
    3 
    4     For Each col As DataGridViewColumn In Workspace.Columns
    5         If arr.Contains(col.Index) Then
    6             col.Visible = False
    7         End If
    8     Next
    9 
   10 End Sub

Custom Text and Menu Rendering

    1 Public Class ThemePainter
    2     Inherits System.Windows.Forms.ToolStripRenderer
    3 
    4     Dim paintBrush As System.Drawing.Brush = Nothing
    5 
    6     Protected Overrides Sub InitializeItem(ByVal item As ToolStripItem)
    7         MyBase.InitializeItem(item)
    8         item.ForeColor = My.Settings.m_foreColor
    9     End Sub
   10 
   11     Protected Overrides Sub OnRenderToolStripBackground(ByVal e As ToolStripRenderEventArgs)
   12         MyBase.OnRenderToolStripBackground(e)
   13         paintBrush = New Drawing2D.LinearGradientBrush(e.ToolStrip.ClientRectangle, _
My
.Settings.m_primaryColor, My.Settings.m_secondaryColor, 90)
   14         
   15         e.Graphics.FillRectangle(Me.paintBrush, e.AffectedBounds)
   16         
   17     End Sub
   18 
   19     Protected Overrides Sub OnRenderItemText(ByVal e As ToolStripItemTextRenderEventArgs)
   20 
   21         Dim flags As TextFormatFlags = TextFormatFlags.NoClipping Or TextFormatFlags.GlyphOverhangPadding               
   22         e.TextColor = My.Settings.m_foreColor
   23         e.TextFont = My.Settings.m_fontFamily
   24 
   25         '{ gotta keep this or it wont paint the whole box }
   26         e.TextFormat = flags
   27 
   28         MyBase.OnRenderItemText(e)
   29 
   30     End Sub
   31 End Class

then called like this:

ToolStrip1.Renderer = New ThemePainter



Processing a file Containing Null characters

    1 Private Sub ReprocessNullfile(ByVal filename As String)
    2         '{ The purpose of this sub is to go through and replace the NULL chars with spaces }
    3         '{ and rewrite a new file to the users Temp folder. The NULL characters mess up the } 
    4         '{ string reading capabilities..... }
    5 
    6         Dim charBuff(globalRecordLength) As Char
    7         Dim fi As New FileInfo(filename)
    8         Dim fileLen As Integer = fi.Length
    9         Dim start, nxt As Integer
   10         Dim ctr As Integer = 0
   11         Dim sr As New System.IO.StreamReader(filename)
   12         Dim sw As New System.IO.StreamWriter(My.Computer.FileSystem.SpecialDirectories.Temp & _
   13             "\temp.txt", False)
   14 
   15         sr.DiscardBufferedData()
   16 
   17         fi = Nothing
   18         start = 0
   19         nxt = globalRecordLength
   20 
   21         While ctr < fileLen
   22             Dim strTest As String = Nothing
   23             sr.Read(charBuff, 0, globalRecordLength)
   24             For x As Integer = 0 To globalRecordLength - 1
   25 
   26                 If charBuff.GetValue(x) = vbNullChar Then
   27                     strTest &= " "
   28                 Else
   29                     strTest &= charBuff.GetValue(x)
   30                 End If
   31                 ctr += 1
   32             Next
   33             sw.WriteLine(strTest)
   34         End While
   35 
   36         sw.Close()
   37 
   38     End Sub

Zipping a File Using Console Version of WinZip

    1 Private Sub DoArchive(ByVal pth As String)
    2     '{ create the new filename. It needs quotes because of potential spaces }
    3     Dim newFilename As String = Chr(34) & pth & Chr(34)
    4 
    5     '{ process info }
    6     Dim p As New Process
    7     Dim s As String
    8 
    9     '{ if the user wants the old directory wiped after archive }
   10     If RemoveOldFilesMenuItem.CheckState = CheckState.Checked Then
   11         s = "-p -r -m """ & pth & ".zip"" """ & pth & """"
   12     Else
   13         '{ otherwise }
   14         s = "-p -r """ & pth & ".zip"" """ & pth & """"
   15     End If
   16 
   17     p.StartInfo.UseShellExecute = False
   18     '{ if we don't do this, we zip files from where the executable is located }
   19     p.StartInfo.WorkingDirectory = pth & "\"
   20     p.StartInfo.Arguments = s
   21     p.StartInfo.CreateNoWindow = True
   22     '{ this is based on the assumption that the Winzip command Line extension is installed }
   23     '{ http://www.winzip.com/downcl.htm }
   24     p.StartInfo.FileName = My.Computer.FileSystem.SpecialDirectories.ProgramFiles.ToString & "\winzip10\wzzip"
   25     p.Start()
   26 
   27 End Sub