Results 1 to 35 of 35

Thread: vb2008 extensions

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    vb2008 extensions


  2. #2

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    this is an extension to the Date class:

    vb Code:
    1. Imports System.Globalization
    2.  
    3. Module extensions
    4.     ''' <summary>
    5.     ''' Date class extension
    6.     ''' </summary>
    7.     ''' <param name="instance"></param>
    8.     ''' <returns>from "3/5/2011" (d/M/yyyy) returns 3rd May 2011</returns>
    9.     ''' <remarks>updated to work with all cultures</remarks>
    10.     <System.Runtime.CompilerServices.Extension()> _
    11.     Public Function toFullDateString(ByVal instance As Date) As String
    12.  
    13.         Dim dateString = instance.ToLongDateString
    14.  
    15.         Dim parts() As String = dateString.Split(" "c)
    16.         Dim dayPart As Integer = Array.FindIndex(parts, AddressOf isDayPart)
    17.  
    18.         parts(dayPart) = parts(dayPart).TrimStart("0"c)
    19.         dateString = String.Join(" ", parts)
    20.  
    21.         Dim insertionPoint As Integer = dateString.IndexOf(parts(dayPart)) + parts(dayPart).TrimEnd(","c).Length
    22.  
    23.         Dim suffix As String = ""
    24.         Select Case CInt(parts(dayPart).TrimEnd(","c))
    25.             Case 1, 21, 31
    26.                 suffix = "st"
    27.             Case 2, 22
    28.                 suffix = "nd"
    29.             Case 3, 23
    30.                 suffix = "rd"
    31.             Case Else
    32.                 suffix = "th"
    33.         End Select
    34.  
    35.         Return dateString.Insert(insertionPoint, suffix)
    36.  
    37.     End Function
    38.  
    39.     Private Function isdaypart(ByVal part As String) As Boolean
    40.         Dim value As Integer
    41.         Integer.TryParse(part.TrimEnd(","c), value)
    42.         Return value > 0 AndAlso value <= 31
    43.     End Function
    44.  
    45. End Module

    edit: updated to work with all cultures
    Last edited by .paul.; Jun 25th, 2010 at 03:09 PM.

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    here's an extension to the DataGridViewButtonCell:

    vb Code:
    1. Module extensions
    2.     Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
    3.     Private Const MOUSEEVENTF_LEFTUP As Integer = &H4
    4.    
    5.     Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, _
    6.                                                   ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
    7.     Private Declare Sub SetCursorPos Lib "User32" (ByVal X As Integer, ByVal Y As Integer)
    8.  
    9.     <System.Runtime.CompilerServices.Extension()> _
    10.     Public Sub performClick(ByVal instance As DataGridViewButtonCell)
    11.         If Not instance.Visible Then Return
    12.         Dim p As Point = instance.DataGridView.PointToScreen(instance.DataGridView.GetCellDisplayRectangle(instance.ColumnIndex, instance.RowIndex, False).Location)
    13.         SetCursorPos(p.X + 10, p.Y + 10)
    14.         mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    15.         mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
    16.     End Sub
    17.  
    18. End Module

  4. #4

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    here's an extension to the string class:

    http://www.vbforums.com/showpost.php...88&postcount=2

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: vb2008 extensions

    Useful extensions, but a bit of explanation and keeping to the .NET naming conventions wouldn't hurt
    (method names start with an uppercase letter and generally do not contain abbreviations such as 'Dup' instead of 'Duplicate')

  6. #6

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    here's 2 extensions to the listbox control (descriptions in the comments Nick):

    vb Code:
    1. Module extensions
    2.     ''' <summary>
    3.     ''' ListBox.itemsArray
    4.     ''' </summary>
    5.     ''' <param name="instance"></param>
    6.     ''' <returns>returns a string array containing the listbox.items as displayed</returns>
    7.     ''' <remarks>works with bound + unbound listboxes</remarks>
    8.     <System.Runtime.CompilerServices.Extension()> _
    9.     Public Function itemsArray(ByVal instance As ListBox) As String()
    10.         If TypeOf instance.Items(0) Is String Then
    11.             Return (From item In instance.Items.Cast(Of String)()).ToArray
    12.         ElseIf TypeOf instance.Items(0) Is Integer Then
    13.             Return Array.ConvertAll((From item In instance.Items.Cast(Of Integer)()).ToArray, Function(i) i.ToString)
    14.         Else
    15.             Return (From drv In instance.Items _
    16.                     Let item = DirectCast(drv, System.Data.DataRowView).Item(instance.DisplayMember).ToString _
    17.                     Select item).ToArray
    18.         End If
    19.     End Function
    20.     ''' <summary>
    21.     ''' ListBox.selectedItemsArray
    22.     ''' </summary>
    23.     ''' <param name="instance"></param>
    24.     ''' <returns>returns a string array containing the listbox.selectedItems as displayed</returns>
    25.     ''' <remarks>works with bound + unbound listboxes</remarks>
    26.     <System.Runtime.CompilerServices.Extension()> _
    27.     Public Function selectedItemsArray(ByVal instance As ListBox) As String()
    28.         If TypeOf instance.Items(0) Is String Then
    29.             Return (From item In instance.SelectedItems.Cast(Of String)()).ToArray
    30.         ElseIf TypeOf instance.Items(0) Is Integer Then
    31.             Return Array.ConvertAll((From item In instance.SelectedItems.Cast(Of Integer)()).ToArray, Function(i) i.ToString)
    32.         Else
    33.             Return (From drv In instance.SelectedItems _
    34.                     Let item = DirectCast(drv, System.Data.DataRowView).Item(instance.DisplayMember).ToString _
    35.                     Select item).ToArray
    36.         End If
    37.     End Function
    38. End Module
    Last edited by .paul.; Jul 15th, 2010 at 01:46 PM.

  7. #7

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    here's 2 extensions to arrays of point():

    vb Code:
    1. Module extensions
    2.  
    3.     ''' <summary>
    4.     ''' min function enum
    5.     ''' </summary>
    6.     ''' <remarks></remarks>
    7.     Public Enum minProperty
    8.         Xmin
    9.         YMin
    10.     End Enum
    11.     ''' <summary>
    12.     ''' extension to the Point() array
    13.     ''' </summary>
    14.     ''' <param name="instance">Point() array</param>
    15.     ''' <param name="property">x or y min</param>
    16.     ''' <returns></returns>
    17.     ''' <remarks>Point</remarks>
    18.     <System.Runtime.CompilerServices.Extension()> _
    19.     Public Function min(ByVal instance As Point(), ByVal [property] As minProperty) As Point
    20.         Dim values() As Integer = Nothing
    21.         Select Case [property]
    22.             Case minProperty.Xmin
    23.                 values = Array.ConvertAll(instance, Function(p) p.X)
    24.             Case minProperty.YMin
    25.                 values = Array.ConvertAll(instance, Function(p) p.Y)
    26.         End Select
    27.         Dim lowest As Integer = values.Min
    28.         Return instance(Array.IndexOf(values, lowest))
    29.     End Function
    30.     ''' <summary>
    31.     ''' max function enum
    32.     ''' </summary>
    33.     ''' <remarks></remarks>
    34.     Public Enum maxProperty
    35.         Xmax
    36.         YMax
    37.     End Enum
    38.     ''' <summary>
    39.     ''' extension to the Point() array
    40.     ''' </summary>
    41.     ''' <param name="instance">Point() array</param>
    42.     ''' <param name="property">x or y max</param>
    43.     ''' <returns>Point</returns>
    44.     ''' <remarks></remarks>
    45.     <System.Runtime.CompilerServices.Extension()> _
    46.     Public Function max(ByVal instance As Point(), ByVal [property] As maxProperty) As Point
    47.         Dim values() As Integer = Nothing
    48.         Select Case [property]
    49.             Case maxProperty.Xmax
    50.                 values = Array.ConvertAll(instance, Function(p) p.X)
    51.             Case maxProperty.YMax
    52.                 values = Array.ConvertAll(instance, Function(p) p.Y)
    53.         End Select
    54.         Dim highest As Integer = values.Max
    55.         Return instance(Array.IndexOf(values, highest))
    56.     End Function
    57.  
    58. End Module

  8. #8
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: vb2008 extensions

    Code:
    ''' <summary>
        ''' ListBox.itemsArray
        ''' </summary>
        ''' <param name="instance"></param>
        ''' <returns>returns a string array containing the listbox.items as displayed</returns>
        ''' <remarks>works with bound + unbound listboxes</remarks>
    Having that in the summary doesn't really help, as the contents of <summary> and <param> are what you see in IntelliSense first.

  9. #9

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    here's a treenodes collection sorter:

    vb Code:
    1. Module extensions
    2.  
    3.     Public Enum sortOrder
    4.         ascending
    5.         descending
    6.     End Enum
    7.  
    8.     <System.Runtime.CompilerServices.Extension()> _
    9.     Public Sub sort(ByVal instance As TreeNodeCollection, ByVal order As sortOrder)
    10.         Dim nodes() As TreeNode = instance.Cast(Of TreeNode).ToArray
    11.         Array.Sort(nodes, Function(x As TreeNode, y As TreeNode) If(order = sortOrder.ascending, x.Text.CompareTo(y.Text), y.Text.CompareTo(x.Text)))
    12.         instance.Clear()
    13.         instance.AddRange(nodes)
    14.     End Sub
    15.  
    16. End Module

  10. #10

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    here's the treenode collection extension with optional childnode sorting:

    vb Code:
    1. Module extensions
    2.  
    3.     Public Enum sortOrder
    4.         ascending
    5.         descending
    6.     End Enum
    7.  
    8.     <System.Runtime.CompilerServices.Extension()> _
    9.     Public Sub sort(ByVal instance As TreeNodeCollection, ByVal order As sortOrder, Optional ByVal sortChildNodes As Boolean = False)
    10.         Dim nodes() As TreeNode = instance.Cast(Of TreeNode).ToArray
    11.         Array.Sort(nodes, Function(x As TreeNode, y As TreeNode) If(order = sortOrder.ascending, x.Text.CompareTo(y.Text), y.Text.CompareTo(x.Text)))
    12.         instance.Clear()
    13.         instance.AddRange(nodes)
    14.         If sortChildNodes Then
    15.             For Each node As TreeNode In nodes
    16.                 node.Nodes.sort(order, True)
    17.             Next
    18.         End If
    19.     End Sub
    20.  
    21. End Module

  11. #11

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    here's an extension to the Long datatype that returns the number as words:

    vb Code:
    1. Imports System.Globalization
    2.  
    3. Module extensions
    4.  
    5.     <System.Runtime.CompilerServices.Extension()> _
    6.     Public Function toNumberString(ByVal instance As Long) As String
    7.         Dim removeAmpersand As Boolean = False
    8.         Dim returnString As String = ""
    9.         Dim trillions As Long = instance \ 1000000000000
    10.         If trillions > 0 Then
    11.             instance -= trillions * 1000000000000
    12.             returnString &= toNumberString(trillions) & " trillion "
    13.         End If
    14.         Dim billions As Long = instance \ 1000000000
    15.         If billions > 0 Then
    16.             instance -= billions * 1000000000
    17.             returnString &= toNumberString(billions) & " billion "
    18.         End If
    19.         Dim millions As Long = instance \ 1000000
    20.         If millions > 0 Then
    21.             instance -= millions * 1000000
    22.             returnString &= toNumberString(millions) & " million "
    23.         End If
    24.         Dim thousands As Long = instance \ 1000
    25.         If thousands > 0 Then
    26.             instance -= thousands * 1000
    27.             returnString &= toNumberString(thousands) & " thousand "
    28.         End If
    29.         Dim hundreds As Long = instance \ 100
    30.         If hundreds > 0 Then
    31.             instance -= hundreds * 100
    32.             returnString &= toNumberString(hundreds) & " hundred "
    33.         End If
    34.         If instance >= 90 Then
    35.             instance -= 90
    36.             returnString &= " & ninety "
    37.             removeAmpersand = True
    38.         End If
    39.         If instance >= 80 Then
    40.             instance -= 80
    41.             returnString &= " & eighty "
    42.             removeAmpersand = True
    43.         End If
    44.         If instance >= 70 Then
    45.             instance -= 70
    46.             returnString &= " & seventy "
    47.             removeAmpersand = True
    48.         End If
    49.         If instance >= 60 Then
    50.             instance -= 60
    51.             returnString &= " & sixty "
    52.             removeAmpersand = True
    53.         End If
    54.         If instance >= 50 Then
    55.             instance -= 50
    56.             returnString &= " & fifty "
    57.             removeAmpersand = True
    58.         End If
    59.         If instance >= 40 Then
    60.             instance -= 40
    61.             returnString &= " & forty "
    62.             removeAmpersand = True
    63.         End If
    64.         If instance >= 30 Then
    65.             instance -= 30
    66.             returnString &= " & thirty "
    67.             removeAmpersand = True
    68.         End If
    69.         If instance >= 20 Then
    70.             instance -= 20
    71.             returnString &= " & twenty "
    72.             removeAmpersand = True
    73.         End If
    74.         If instance = 19 Then
    75.             returnString &= " & nineteen"
    76.             instance = 0
    77.         End If
    78.         If instance = 18 Then
    79.             returnString &= " & eighteen"
    80.             instance = 0
    81.         End If
    82.         If instance = 17 Then
    83.             returnString &= " & seventeen"
    84.             instance = 0
    85.         End If
    86.         If instance = 16 Then
    87.             returnString &= " & sixteen"
    88.             instance = 0
    89.         End If
    90.         If instance = 15 Then
    91.             returnString &= " & fifteen"
    92.             instance = 0
    93.         End If
    94.         If instance = 14 Then
    95.             returnString &= " & fourteen"
    96.             instance = 0
    97.         End If
    98.         If instance = 13 Then
    99.             returnString &= " & thirteen"
    100.             instance = 0
    101.         End If
    102.         If instance = 12 Then
    103.             returnString &= " & twelve"
    104.             instance = 0
    105.         End If
    106.         If instance = 11 Then
    107.             returnString &= " & eleven"
    108.             instance = 0
    109.         End If
    110.         If instance = 10 Then
    111.             returnString &= " & ten"
    112.             instance = 0
    113.         End If
    114.         If instance = 9 Then
    115.             returnString &= If(removeAmpersand, " nine", " & nine")
    116.             instance = 0
    117.         End If
    118.         If instance = 8 Then
    119.             returnString &= If(removeAmpersand, " eight", " & eight")
    120.             instance = 0
    121.         End If
    122.         If instance = 7 Then
    123.             returnString &= If(removeAmpersand, " seven", " & seven")
    124.             instance = 0
    125.         End If
    126.         If instance = 6 Then
    127.             returnString &= If(removeAmpersand, " six", " & six")
    128.             instance = 0
    129.         End If
    130.         If instance = 5 Then
    131.             returnString &= If(removeAmpersand, " five", " & five")
    132.             instance = 0
    133.         End If
    134.         If instance = 4 Then
    135.             returnString &= If(removeAmpersand, " four", " & four")
    136.             instance = 0
    137.         End If
    138.         If instance = 3 Then
    139.             returnString &= If(removeAmpersand, " three", " & three")
    140.             instance = 0
    141.         End If
    142.         If instance = 2 Then
    143.             returnString &= If(removeAmpersand, " two", " & two")
    144.             instance = 0
    145.         End If
    146.         If instance = 1 Then
    147.             returnString &= If(removeAmpersand, " one", " & one")
    148.             instance = 0
    149.         End If
    150.  
    151.         Return returnString.TrimStart(New Char() {" "c, "&"c}).Replace("  ", " ").Trim
    152.  
    153.     End Function
    154.  
    155. End Module

  12. #12
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: vb2008 extensions

    Quote Originally Posted by .paul. View Post
    here's an extension to the Long datatype that returns the number as words:

    vb Code:
    1. Imports System.Globalization
    2.  
    3. Module extensions
    4.  
    5.     <System.Runtime.CompilerServices.Extension()> _
    6.     Public Function toNumberString(ByVal instance As Long) As String
    7.         Dim removeAmpersand As Boolean = False
    8.         Dim returnString As String = ""
    9.         Dim trillions As Long = instance \ 1000000000000
    10.         If trillions > 0 Then
    11.             instance -= trillions * 1000000000000
    12.             returnString &= toNumberString(trillions) & " trillion "
    13.         End If
    14.         Dim billions As Long = instance \ 1000000000
    15.         If billions > 0 Then
    16.             instance -= billions * 1000000000
    17.             returnString &= toNumberString(billions) & " billion "
    18.         End If
    19.         Dim millions As Long = instance \ 1000000
    20.         If millions > 0 Then
    21.             instance -= millions * 1000000
    22.             returnString &= toNumberString(millions) & " million "
    23.         End If
    24.         Dim thousands As Long = instance \ 1000
    25.         If thousands > 0 Then
    26.             instance -= thousands * 1000
    27.             returnString &= toNumberString(thousands) & " thousand "
    28.         End If
    29.         Dim hundreds As Long = instance \ 100
    30.         If hundreds > 0 Then
    31.             instance -= hundreds * 100
    32.             returnString &= toNumberString(hundreds) & " hundred "
    33.         End If
    34.         If instance >= 90 Then
    35.             instance -= 90
    36.             returnString &= " & ninety "
    37.             removeAmpersand = True
    38.         End If
    39.         If instance >= 80 Then
    40.             instance -= 80
    41.             returnString &= " & eighty "
    42.             removeAmpersand = True
    43.         End If
    44.         If instance >= 70 Then
    45.             instance -= 70
    46.             returnString &= " & seventy "
    47.             removeAmpersand = True
    48.         End If
    49.         If instance >= 60 Then
    50.             instance -= 60
    51.             returnString &= " & sixty "
    52.             removeAmpersand = True
    53.         End If
    54.         If instance >= 50 Then
    55.             instance -= 50
    56.             returnString &= " & fifty "
    57.             removeAmpersand = True
    58.         End If
    59.         If instance >= 40 Then
    60.             instance -= 40
    61.             returnString &= " & forty "
    62.             removeAmpersand = True
    63.         End If
    64.         If instance >= 30 Then
    65.             instance -= 30
    66.             returnString &= " & thirty "
    67.             removeAmpersand = True
    68.         End If
    69.         If instance >= 20 Then
    70.             instance -= 20
    71.             returnString &= " & twenty "
    72.             removeAmpersand = True
    73.         End If
    74.         If instance = 19 Then
    75.             returnString &= " & nineteen"
    76.             instance = 0
    77.         End If
    78.         If instance = 18 Then
    79.             returnString &= " & eighteen"
    80.             instance = 0
    81.         End If
    82.         If instance = 17 Then
    83.             returnString &= " & seventeen"
    84.             instance = 0
    85.         End If
    86.         If instance = 16 Then
    87.             returnString &= " & sixteen"
    88.             instance = 0
    89.         End If
    90.         If instance = 15 Then
    91.             returnString &= " & fifteen"
    92.             instance = 0
    93.         End If
    94.         If instance = 14 Then
    95.             returnString &= " & fourteen"
    96.             instance = 0
    97.         End If
    98.         If instance = 13 Then
    99.             returnString &= " & thirteen"
    100.             instance = 0
    101.         End If
    102.         If instance = 12 Then
    103.             returnString &= " & twelve"
    104.             instance = 0
    105.         End If
    106.         If instance = 11 Then
    107.             returnString &= " & eleven"
    108.             instance = 0
    109.         End If
    110.         If instance = 10 Then
    111.             returnString &= " & ten"
    112.             instance = 0
    113.         End If
    114.         If instance = 9 Then
    115.             returnString &= If(removeAmpersand, " nine", " & nine")
    116.             instance = 0
    117.         End If
    118.         If instance = 8 Then
    119.             returnString &= If(removeAmpersand, " eight", " & eight")
    120.             instance = 0
    121.         End If
    122.         If instance = 7 Then
    123.             returnString &= If(removeAmpersand, " seven", " & seven")
    124.             instance = 0
    125.         End If
    126.         If instance = 6 Then
    127.             returnString &= If(removeAmpersand, " six", " & six")
    128.             instance = 0
    129.         End If
    130.         If instance = 5 Then
    131.             returnString &= If(removeAmpersand, " five", " & five")
    132.             instance = 0
    133.         End If
    134.         If instance = 4 Then
    135.             returnString &= If(removeAmpersand, " four", " & four")
    136.             instance = 0
    137.         End If
    138.         If instance = 3 Then
    139.             returnString &= If(removeAmpersand, " three", " & three")
    140.             instance = 0
    141.         End If
    142.         If instance = 2 Then
    143.             returnString &= If(removeAmpersand, " two", " & two")
    144.             instance = 0
    145.         End If
    146.         If instance = 1 Then
    147.             returnString &= If(removeAmpersand, " one", " & one")
    148.             instance = 0
    149.         End If
    150.  
    151.         Return returnString.TrimStart(New Char() {" "c, "&"c}).Replace("  ", " ").Trim
    152.  
    153.     End Function
    154.  
    155. End Module
    Have you ever heard of loops and arrays? That could be trimmed down quite a bit.
    Last edited by minitech; May 8th, 2011 at 07:18 PM.

  13. #13

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    Quote Originally Posted by minitech View Post
    Have you ever heard of loops and arrays? That could be trimmed down quite a bit.
    if you can improve it, you're welcome to post it in this thread

  14. #14
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: vb2008 extensions

    Sure, see CodeBank thread.
    Last edited by minitech; May 9th, 2011 at 02:51 PM.

  15. #15

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    i haven't tried it but i don't see how 167 lines is trimmed down compared to my 155 lines

  16. #16
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: vb2008 extensions

    Well, I added negative numbers, -illions up to sextillions, fractions, decimal points, etc.

  17. #17

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    here's some extensions for 2d integer arrays:

    vb Code:
    1. Module extensions
    2.     ''' <summary>
    3.     ''' 2d integer array sum function
    4.     ''' </summary>
    5.     ''' <param name="instance"></param>
    6.     ''' <returns>sum of all elements</returns>
    7.     ''' <remarks></remarks>
    8.     <System.Runtime.CompilerServices.Extension()> _
    9.     Public Function sum(ByVal instance(,) As Integer) As Integer
    10.         Return Enumerable.Range(0, instance.GetUpperBound(0) + 1).Select(Function(i1) Enumerable.Range(0, instance.GetUpperBound(1) + 1).Select(Function(i2) instance(i1, i2)).Sum).Sum
    11.     End Function
    12.     ''' <summary>
    13.     ''' 2d integer array sumColumn function
    14.     ''' </summary>
    15.     ''' <param name="instance"></param>
    16.     ''' <param name="columnIndex"></param>
    17.     ''' <returns>sum of column</returns>
    18.     ''' <remarks></remarks>
    19.     <System.Runtime.CompilerServices.Extension()> _
    20.     Public Function sumColumn(ByVal instance(,) As Integer, ByVal columnIndex As Integer) As Integer
    21.         Return Enumerable.Range(0, instance.GetUpperBound(0) + 1).Select(Function(i) instance(i, columnIndex)).Sum
    22.     End Function
    23.     ''' <summary>
    24.     ''' 2d integer array sumRow function
    25.     ''' </summary>
    26.     ''' <param name="instance"></param>
    27.     ''' <param name="rowIndex"></param>
    28.     ''' <returns>sum of row</returns>
    29.     ''' <remarks></remarks>
    30.     <System.Runtime.CompilerServices.Extension()> _
    31.     Public Function sumRow(ByVal instance(,) As Integer, ByVal rowIndex As Integer) As Integer
    32.         Return Enumerable.Range(0, instance.GetUpperBound(1) + 1).Select(Function(i) instance(rowIndex, i)).Sum
    33.     End Function
    34. End Module

  18. #18

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    this is a vb2010 extension for reading a csv file into a 2d array:

    vb Code:
    1. Module extensions
    2.  
    3.     ''' <summary>
    4.     ''' to2DStringArray
    5.     ''' </summary>
    6.     ''' <param name="instance">a 1D string array</param>
    7.     ''' <returns>a 2D string array</returns>
    8.     ''' <remarks></remarks>
    9.     <System.Runtime.CompilerServices.Extension()> _
    10.     Public Function to2DStringArray(ByVal instance As String()) As String(,)
    11.         Dim csvArray(instance.GetUpperBound(0), instance(0).Split(","c).GetUpperBound(0)) As String
    12.         Array.ForEach(Enumerable.Range(0, csvArray.GetUpperBound(0) + 1).ToArray, Sub(r) Array.ForEach(Enumerable.Range(0, csvArray.GetUpperBound(1) + 1).ToArray, Sub(c) csvArray(r, c) = instance(r).Split(","c)(c)))
    13.         Return csvArray
    14.     End Function
    15.  
    16. End Module

    you use it like this:

    vb Code:
    1. Dim csv(,) As String = IO.File.ReadAllLines("csv.txt").to2DStringArray()

  19. #19

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    here's a vb2008 extension that finds all controls oftype + adds them to a list(of T):

    Code:
    Module extensions
    
        <System.Runtime.CompilerServices.Extension()> _
        Public Sub findNestedControls(Of T)(ByVal instance As Control, ByVal ctrls As List(Of T))
            ctrls.AddRange(instance.Controls.OfType(Of T).ToArray)
            For Each child As Control In instance.Controls
                child.findNestedControls(Of T)(ctrls)
            Next
        End Sub
    
    End Module
    this is how you'd use it:

    Code:
    Dim textboxes As New List(Of TextBox)
    Me.findNestedControls(Of TextBox)(textboxes)

  20. #20
    Lively Member
    Join Date
    Sep 2013
    Posts
    117

    Re: vb2008 extensions

    Produces incorrect results when the values are quoted, eg
    Code:
     ",",","
    Why not just use the TextFieldParser?

  21. #21

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    Quote Originally Posted by AdamPanic2013 View Post
    Produces incorrect results when the values are quoted, eg
    Code:
     ",",","
    Why not just use the TextFieldParser?
    which post are you referring to?

  22. #22
    Lively Member
    Join Date
    Sep 2013
    Posts
    117

    Re: vb2008 extensions

    Quote Originally Posted by .paul. View Post
    which post are you referring to?
    Post 18

  23. #23
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: vb2008 extensions

    Re: toFullDateString: Wouldn’t something like this work?

    Code:
    Return Me.Day.OrdinalSuffix() & Me.ToString(" MMMM yyyy")

  24. #24

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    Quote Originally Posted by minitech View Post
    Re: toFullDateString: Wouldn’t something like this work?

    Code:
    Return Me.Day.OrdinalSuffix() & Me.ToString(" MMMM yyyy")
    I don't follow...?
    can you explain?

  25. #25
    Lively Member
    Join Date
    Sep 2013
    Posts
    117

    Re: vb2008 extensions

    Extracted out the ordinal aspect out into it own function. OrdinalSuffix
    Code:
    Namespace Global.Exts.Ordinals
      <HideModuleName>
    Public Module Exts
      <Extension> Public Function OrdinalSuffix( value As Integer) As String
        If value < 0 Then Return ""
        If ((value \ 10) Mod 10)=1 Then Return "th"
        Select Case value
          Case 1:Return "st"
          Case 2:Return "nd"
          Case 3:Return "rd"
        End select
        Return "th"
      End Function
    Now the ToFullDateString
    Code:
      <Extension> Public Function ToFullDateString(d As Date) As String
         return String.Format("{0}{1} {2}",d.Day,  d.Day.OrdinalSuffix ,d.ToString("MMMM yyyy"))
       End Function
    End Module 
    End Namespace
    And example usage

    Code:
     Dim dd = #09/27/1979#.ToFullDateString ' That's a US date literal

  26. #26
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: vb2008 extensions

    What he said. Except

    Code:
    Return String.Format("{0:d}{1} {0:MMMM yyyy}", d.Day.OrdinalSuffix(), d)
    if that works. I don’t know.

  27. #27

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    Quote Originally Posted by AdamPanic2013 View Post
    Extracted out the ordinal aspect out into it own function. OrdinalSuffix
    Code:
    Namespace Global.Exts.Ordinals
      <HideModuleName>
    Public Module Exts
      <Extension> Public Function OrdinalSuffix( value As Integer) As String
        If value < 0 Then Return ""
        If ((value \ 10) Mod 10)=1 Then Return "th"
        Select Case value
          Case 1:Return "st"
          Case 2:Return "nd"
          Case 3:Return "rd"
        End select
        Return "th"
      End Function
    Now the ToFullDateString
    Code:
      <Extension> Public Function ToFullDateString(d As Date) As String
         return String.Format("{0}{1} {2}",d.Day,  d.Day.OrdinalSuffix ,d.ToString("MMMM yyyy"))
       End Function
    End Module 
    End Namespace
    And example usage

    Code:
     Dim dd = #09/27/1979#.ToFullDateString ' That's a US date literal
    Quote Originally Posted by minitech View Post
    What he said. Except

    Code:
    Return String.Format("{0:d}{1} {0:MMMM yyyy}", d.Day.OrdinalSuffix(), d)
    if that works. I don’t know.
    well yeah. whatever way you want to use it. it's open to interpretation
    that's something Microsoft should include in the next version of VS

    edit: ok I tried it. your calculations are out there. try:

    Code:
    MsgBox(#9/21/1979#.ToFullDateString)
    unless i'm very much mistaken, the 21th September 1979 never existed in this dimension

    why change working code?:

    Code:
    Select Case value
        Case 1, 21, 31: Return "st"
        Case 2, 22: Return "nd"
        Case 3, 23: Return "rd"
        Case Else: Return "th"
    End Select
    Last edited by .paul.; Sep 27th, 2013 at 03:36 PM.

  28. #28

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    Quote Originally Posted by minitech View Post
    What he said. Except

    Code:
    Return String.Format("{0:d}{1} {0:MMMM yyyy}", d.Day.OrdinalSuffix(), d)
    if that works. I don’t know.
    AdamPanic2013's OrdinalSuffix function doesn't work, which leads me to ask: why are you spamming my thread???

  29. #29
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: vb2008 extensions

    I’m not spamming your thread. You can make your own OrdinalSuffix extension. I was just saying that custom date formats are built into .NET; you don’t have to format it one way, split it, shuffle it, find and parse out a likely day, calculate a suffix, and put that all together just to get a date in a particular format.

  30. #30

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    I can't remember exactly why I did it that way.
    I think it was to include the day name in the output string

  31. #31

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    Quote Originally Posted by minitech View Post
    I’m not spamming your thread. You can make your own OrdinalSuffix extension. I was just saying that custom date formats are built into .NET; you don’t have to format it one way, split it, shuffle it, find and parse out a likely day, calculate a suffix, and put that all together just to get a date in a particular format.
    Quote Originally Posted by .paul. View Post
    I can't remember exactly why I did it that way.
    I think it was to include the day name in the output string
    ok. perhaps it was due for an overhaul


    Code:
    Module extensions
        ''' <summary>
        ''' Date class extension
        ''' </summary>
        ''' <param name="instance"></param>
        ''' <returns>from "3/5/2011" (d/M/yyyy) returns Tuesday 3rd May 2011</returns>
        ''' <remarks></remarks>
        <System.Runtime.CompilerServices.Extension()> _
        Public Function toFullDateString(ByVal instance As Date, Optional ByVal era As Boolean = False, Optional ByVal utcOffset As Boolean = False) As String
            Return String.Format("{0:dddd} {0:%d}{1} {0:MMMM} {0:yyyy}" & If(era, " {0:gg}", "") & If(utcOffset, " {0:zzz}", ""), instance, OrdinalSuffix(instance.Day))
        End Function
    
        Private Function OrdinalSuffix(ByVal d As Integer) As String
            Select Case d
                Case 1, 21, 31 : Return "st"
                Case 2, 22 : Return "nd"
                Case 3, 23 : Return "rd"
                Case Else : Return "th"
            End Select
        End Function
    
    End Module
    Last edited by .paul.; Sep 27th, 2013 at 06:17 PM.

  32. #32
    Lively Member
    Join Date
    Sep 2013
    Posts
    117

    Re: vb2008 extensions

    .paul. your Ordinal only covers values 1 to 31.
    A small correction to mine and it covers any ordinal
    Code:
    Public Module Exts
      <Extension> Public Function OrdinalSuffix( value As Integer) As String
        If value < 0 then Return ""
        If ((value \ 10) Mod 10)=1 Then Return "th"
        Select Case value Mod 10
          Case 1:Return "st"
          Case 2:Return "nd"
          Case 3:Return "rd"
        End select
        Return "th"
      End Function

  33. #33

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    Quote Originally Posted by minitech View Post
    I’m not spamming your thread. You can make your own OrdinalSuffix extension. I was just saying that custom date formats are built into .NET; you don’t have to format it one way, split it, shuffle it, find and parse out a likely day, calculate a suffix, and put that all together just to get a date in a particular format.
    I remember now. ToLongDateString returns a differently formatted date string depending where you are in the world.
    It was an attempt to retain that format, but I admit it was scrappy code.

  34. #34

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    Code:
    Imports System.Runtime.CompilerServices
    Imports System.Text.RegularExpressions
    
    Module extensions
        ''' <summary>
        ''' Date class extension
        ''' </summary>
        ''' <param name="instance"></param>
        ''' <returns>from "3/5/2011" (d/M/yyyy) returns Tuesday 3rd May 2011</returns>
        ''' <remarks></remarks>
        <Extension()> _
        Public Function toFullDateString(ByVal instance As Date, Optional ByVal era As Boolean = False, Optional ByVal utcOffset As Boolean = False) As String
            Return Regex.Replace(instance.ToLongDateString, String.Format("\b0?{0}\b", instance.Day), String.Format("{0:%d}{1}", instance, OrdinalSuffix(instance.Day)))
        End Function
    
        Private Function OrdinalSuffix(ByVal d As Integer) As String
            Select Case d
                Case 1, 21, 31 : Return "st"
                Case 2, 22 : Return "nd"
                Case 3, 23 : Return "rd"
                Case Else : Return "th"
            End Select
        End Function
    
    End Module

  35. #35

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: vb2008 extensions

    here's another extension to the treeview control for expanding + collapsing by node level:

    Code:
    Imports System.Runtime.CompilerServices
    
    Module extensions
        <Extension()> _
        Public Sub expand(ByVal instance As TreeView, ByVal depth As Integer)
            instance.CollapseAll()
            recursiveExpand(instance.Nodes, depth, 0)
        End Sub
    
        Private Sub recursiveExpand(ByVal t As TreeNodeCollection, ByVal depth As Integer, ByVal startAt As Integer)
            For x As Integer = startAt To depth - 1
                For Each node As TreeNode In t.Cast(Of TreeNode).Where(Function(n) n.Level = x)
                    node.Expand()
                    recursiveExpand(node.Nodes, depth, x + 1)
                Next
            Next
        End Sub
    
    End Module
    this is how to use it:

    Code:
    Public Class Form1
    
        Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
            TreeView1.expand(CInt(NumericUpDown1.Value))
        End Sub
    
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width