Visual Basic Helper Routines
StrFormatByteSize: Convert Numbers to Strings Expressed as Size Values
     
Posted:   Monday March 03, 2003
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP6
OS restrictions:   None
Author:   VBnet - Randy Birch
     
 Prerequisites
32-bit VB.
Straightforward routine to convert a numeric value into a string that represents the number expressed as a size value in bytes, kilobytes, megabytes, or gigabytes, depending on the size. Note the API takes a long as input, so the maximum value must not exceed 2147483647 (1.99 gigabytes).

 Form Code
Add the following to a form containing a command button:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function StrFormatByteSize Lib "shlwapi" _
   Alias "StrFormatByteSizeA" _
  (ByVal dw As Long, _
   ByVal pszBuf As String, _
   ByVal cchBuf As Long) As Long


Private Sub Command1_Click()

  'note: the IN value of the API is a long
   Print FormatByteSize(2147483147)
   Print FormatByteSize(37423218.34)
   Print FormatByteSize(3742321.34)
   Print FormatByteSize(374232.34)
   Print FormatByteSize(37423.34)
   Print FormatByteSize(3742.34)
   Print FormatByteSize(1024)
   Print FormatByteSize(1023)
   Print FormatByteSize(374.34)
   Print FormatByteSize(37.34)
   
End Sub


Private Function FormatByteSize(dwBytes As Single) As String

   Dim sBuff As String
   Dim dwBuff As Long
   
   sBuff = Space$(32)
   dwBuff = Len(sBuff)
   
   If StrFormatByteSize(dwBytes, _
                        sBuff, _
                        dwBuff) <> 0 Then
                     
      FormatByteSize = Left$(sBuff, _
                             InStr(sBuff, _
                             Chr$(0)) - 1)
   
   End If
   
   
End Function
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter