|
-
Jun 25th, 2010, 01:19 PM
#1
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 25th, 2010, 01:20 PM
#2
Re: vb2008 extensions
this is an extension to the Date class:
vb Code:
Imports System.Globalization
Module extensions
''' <summary>
''' Date class extension
''' </summary>
''' <param name="instance"></param>
''' <returns>from "3/5/2011" (d/M/yyyy) returns 3rd May 2011</returns>
''' <remarks>updated to work with all cultures</remarks>
<System.Runtime.CompilerServices.Extension()> _
Public Function toFullDateString(ByVal instance As Date) As String
Dim dateString = instance.ToLongDateString
Dim parts() As String = dateString.Split(" "c)
Dim dayPart As Integer = Array.FindIndex(parts, AddressOf isDayPart)
parts(dayPart) = parts(dayPart).TrimStart("0"c)
dateString = String.Join(" ", parts)
Dim insertionPoint As Integer = dateString.IndexOf(parts(dayPart)) + parts(dayPart).TrimEnd(","c).Length
Dim suffix As String = ""
Select Case CInt(parts(dayPart).TrimEnd(","c))
Case 1, 21, 31
suffix = "st"
Case 2, 22
suffix = "nd"
Case 3, 23
suffix = "rd"
Case Else
suffix = "th"
End Select
Return dateString.Insert(insertionPoint, suffix)
End Function
Private Function isdaypart(ByVal part As String) As Boolean
Dim value As Integer
Integer.TryParse(part.TrimEnd(","c), value)
Return value > 0 AndAlso value <= 31
End Function
End Module
edit: updated to work with all cultures
Last edited by .paul.; Jun 25th, 2010 at 03:09 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 27th, 2010, 01:00 PM
#3
Re: vb2008 extensions
here's an extension to the DataGridViewButtonCell:
vb Code:
Module extensions
Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
Private Const MOUSEEVENTF_LEFTUP As Integer = &H4
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, _
ByVal cButtons As Integer, ByVal dwExtraInfo As Integer)
Private Declare Sub SetCursorPos Lib "User32" (ByVal X As Integer, ByVal Y As Integer)
<System.Runtime.CompilerServices.Extension()> _
Public Sub performClick(ByVal instance As DataGridViewButtonCell)
If Not instance.Visible Then Return
Dim p As Point = instance.DataGridView.PointToScreen(instance.DataGridView.GetCellDisplayRectangle(instance.ColumnIndex, instance.RowIndex, False).Location)
SetCursorPos(p.X + 10, p.Y + 10)
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub
End Module
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 10th, 2010, 05:22 PM
#4
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jul 12th, 2010, 07:52 AM
#5
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')
-
Jul 15th, 2010, 12:18 PM
#6
Re: vb2008 extensions
here's 2 extensions to the listbox control (descriptions in the comments Nick):
vb Code:
Module extensions
''' <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>
<System.Runtime.CompilerServices.Extension()> _
Public Function itemsArray(ByVal instance As ListBox) As String()
If TypeOf instance.Items(0) Is String Then
Return (From item In instance.Items.Cast(Of String)()).ToArray
ElseIf TypeOf instance.Items(0) Is Integer Then
Return Array.ConvertAll((From item In instance.Items.Cast(Of Integer)()).ToArray, Function(i) i.ToString)
Else
Return (From drv In instance.Items _
Let item = DirectCast(drv, System.Data.DataRowView).Item(instance.DisplayMember).ToString _
Select item).ToArray
End If
End Function
''' <summary>
''' ListBox.selectedItemsArray
''' </summary>
''' <param name="instance"></param>
''' <returns>returns a string array containing the listbox.selectedItems as displayed</returns>
''' <remarks>works with bound + unbound listboxes</remarks>
<System.Runtime.CompilerServices.Extension()> _
Public Function selectedItemsArray(ByVal instance As ListBox) As String()
If TypeOf instance.Items(0) Is String Then
Return (From item In instance.SelectedItems.Cast(Of String)()).ToArray
ElseIf TypeOf instance.Items(0) Is Integer Then
Return Array.ConvertAll((From item In instance.SelectedItems.Cast(Of Integer)()).ToArray, Function(i) i.ToString)
Else
Return (From drv In instance.SelectedItems _
Let item = DirectCast(drv, System.Data.DataRowView).Item(instance.DisplayMember).ToString _
Select item).ToArray
End If
End Function
End Module
Last edited by .paul.; Jul 15th, 2010 at 01:46 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 14th, 2010, 07:35 PM
#7
Re: vb2008 extensions
here's 2 extensions to arrays of point():
vb Code:
Module extensions
''' <summary>
''' min function enum
''' </summary>
''' <remarks></remarks>
Public Enum minProperty
Xmin
YMin
End Enum
''' <summary>
''' extension to the Point() array
''' </summary>
''' <param name="instance">Point() array</param>
''' <param name="property">x or y min</param>
''' <returns></returns>
''' <remarks>Point</remarks>
<System.Runtime.CompilerServices.Extension()> _
Public Function min(ByVal instance As Point(), ByVal [property] As minProperty) As Point
Dim values() As Integer = Nothing
Select Case [property]
Case minProperty.Xmin
values = Array.ConvertAll(instance, Function(p) p.X)
Case minProperty.YMin
values = Array.ConvertAll(instance, Function(p) p.Y)
End Select
Dim lowest As Integer = values.Min
Return instance(Array.IndexOf(values, lowest))
End Function
''' <summary>
''' max function enum
''' </summary>
''' <remarks></remarks>
Public Enum maxProperty
Xmax
YMax
End Enum
''' <summary>
''' extension to the Point() array
''' </summary>
''' <param name="instance">Point() array</param>
''' <param name="property">x or y max</param>
''' <returns>Point</returns>
''' <remarks></remarks>
<System.Runtime.CompilerServices.Extension()> _
Public Function max(ByVal instance As Point(), ByVal [property] As maxProperty) As Point
Dim values() As Integer = Nothing
Select Case [property]
Case maxProperty.Xmax
values = Array.ConvertAll(instance, Function(p) p.X)
Case maxProperty.YMax
values = Array.ConvertAll(instance, Function(p) p.Y)
End Select
Dim highest As Integer = values.Max
Return instance(Array.IndexOf(values, highest))
End Function
End Module
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 17th, 2010, 06:59 PM
#8
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.
-
Feb 4th, 2011, 06:53 PM
#9
Re: vb2008 extensions
here's a treenodes collection sorter:
vb Code:
Module extensions
Public Enum sortOrder
ascending
descending
End Enum
<System.Runtime.CompilerServices.Extension()> _
Public Sub sort(ByVal instance As TreeNodeCollection, ByVal order As sortOrder)
Dim nodes() As TreeNode = instance.Cast(Of TreeNode).ToArray
Array.Sort(nodes, Function(x As TreeNode, y As TreeNode) If(order = sortOrder.ascending, x.Text.CompareTo(y.Text), y.Text.CompareTo(x.Text)))
instance.Clear()
instance.AddRange(nodes)
End Sub
End Module
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 4th, 2011, 09:51 PM
#10
Re: vb2008 extensions
here's the treenode collection extension with optional childnode sorting:
vb Code:
Module extensions
Public Enum sortOrder
ascending
descending
End Enum
<System.Runtime.CompilerServices.Extension()> _
Public Sub sort(ByVal instance As TreeNodeCollection, ByVal order As sortOrder, Optional ByVal sortChildNodes As Boolean = False)
Dim nodes() As TreeNode = instance.Cast(Of TreeNode).ToArray
Array.Sort(nodes, Function(x As TreeNode, y As TreeNode) If(order = sortOrder.ascending, x.Text.CompareTo(y.Text), y.Text.CompareTo(x.Text)))
instance.Clear()
instance.AddRange(nodes)
If sortChildNodes Then
For Each node As TreeNode In nodes
node.Nodes.sort(order, True)
Next
End If
End Sub
End Module
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 8th, 2011, 06:14 PM
#11
Re: vb2008 extensions
here's an extension to the Long datatype that returns the number as words:
vb Code:
Imports System.Globalization
Module extensions
<System.Runtime.CompilerServices.Extension()> _
Public Function toNumberString(ByVal instance As Long) As String
Dim removeAmpersand As Boolean = False
Dim returnString As String = ""
Dim trillions As Long = instance \ 1000000000000
If trillions > 0 Then
instance -= trillions * 1000000000000
returnString &= toNumberString(trillions) & " trillion "
End If
Dim billions As Long = instance \ 1000000000
If billions > 0 Then
instance -= billions * 1000000000
returnString &= toNumberString(billions) & " billion "
End If
Dim millions As Long = instance \ 1000000
If millions > 0 Then
instance -= millions * 1000000
returnString &= toNumberString(millions) & " million "
End If
Dim thousands As Long = instance \ 1000
If thousands > 0 Then
instance -= thousands * 1000
returnString &= toNumberString(thousands) & " thousand "
End If
Dim hundreds As Long = instance \ 100
If hundreds > 0 Then
instance -= hundreds * 100
returnString &= toNumberString(hundreds) & " hundred "
End If
If instance >= 90 Then
instance -= 90
returnString &= " & ninety "
removeAmpersand = True
End If
If instance >= 80 Then
instance -= 80
returnString &= " & eighty "
removeAmpersand = True
End If
If instance >= 70 Then
instance -= 70
returnString &= " & seventy "
removeAmpersand = True
End If
If instance >= 60 Then
instance -= 60
returnString &= " & sixty "
removeAmpersand = True
End If
If instance >= 50 Then
instance -= 50
returnString &= " & fifty "
removeAmpersand = True
End If
If instance >= 40 Then
instance -= 40
returnString &= " & forty "
removeAmpersand = True
End If
If instance >= 30 Then
instance -= 30
returnString &= " & thirty "
removeAmpersand = True
End If
If instance >= 20 Then
instance -= 20
returnString &= " & twenty "
removeAmpersand = True
End If
If instance = 19 Then
returnString &= " & nineteen"
instance = 0
End If
If instance = 18 Then
returnString &= " & eighteen"
instance = 0
End If
If instance = 17 Then
returnString &= " & seventeen"
instance = 0
End If
If instance = 16 Then
returnString &= " & sixteen"
instance = 0
End If
If instance = 15 Then
returnString &= " & fifteen"
instance = 0
End If
If instance = 14 Then
returnString &= " & fourteen"
instance = 0
End If
If instance = 13 Then
returnString &= " & thirteen"
instance = 0
End If
If instance = 12 Then
returnString &= " & twelve"
instance = 0
End If
If instance = 11 Then
returnString &= " & eleven"
instance = 0
End If
If instance = 10 Then
returnString &= " & ten"
instance = 0
End If
If instance = 9 Then
returnString &= If(removeAmpersand, " nine", " & nine")
instance = 0
End If
If instance = 8 Then
returnString &= If(removeAmpersand, " eight", " & eight")
instance = 0
End If
If instance = 7 Then
returnString &= If(removeAmpersand, " seven", " & seven")
instance = 0
End If
If instance = 6 Then
returnString &= If(removeAmpersand, " six", " & six")
instance = 0
End If
If instance = 5 Then
returnString &= If(removeAmpersand, " five", " & five")
instance = 0
End If
If instance = 4 Then
returnString &= If(removeAmpersand, " four", " & four")
instance = 0
End If
If instance = 3 Then
returnString &= If(removeAmpersand, " three", " & three")
instance = 0
End If
If instance = 2 Then
returnString &= If(removeAmpersand, " two", " & two")
instance = 0
End If
If instance = 1 Then
returnString &= If(removeAmpersand, " one", " & one")
instance = 0
End If
Return returnString.TrimStart(New Char() {" "c, "&"c}).Replace(" ", " ").Trim
End Function
End Module
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 8th, 2011, 06:20 PM
#12
Re: vb2008 extensions
 Originally Posted by .paul.
here's an extension to the Long datatype that returns the number as words:
vb Code:
Imports System.Globalization Module extensions <System.Runtime.CompilerServices.Extension()> _ Public Function toNumberString(ByVal instance As Long) As String Dim removeAmpersand As Boolean = False Dim returnString As String = "" Dim trillions As Long = instance \ 1000000000000 If trillions > 0 Then instance -= trillions * 1000000000000 returnString &= toNumberString(trillions) & " trillion " End If Dim billions As Long = instance \ 1000000000 If billions > 0 Then instance -= billions * 1000000000 returnString &= toNumberString(billions) & " billion " End If Dim millions As Long = instance \ 1000000 If millions > 0 Then instance -= millions * 1000000 returnString &= toNumberString(millions) & " million " End If Dim thousands As Long = instance \ 1000 If thousands > 0 Then instance -= thousands * 1000 returnString &= toNumberString(thousands) & " thousand " End If Dim hundreds As Long = instance \ 100 If hundreds > 0 Then instance -= hundreds * 100 returnString &= toNumberString(hundreds) & " hundred " End If If instance >= 90 Then instance -= 90 returnString &= " & ninety " removeAmpersand = True End If If instance >= 80 Then instance -= 80 returnString &= " & eighty " removeAmpersand = True End If If instance >= 70 Then instance -= 70 returnString &= " & seventy " removeAmpersand = True End If If instance >= 60 Then instance -= 60 returnString &= " & sixty " removeAmpersand = True End If If instance >= 50 Then instance -= 50 returnString &= " & fifty " removeAmpersand = True End If If instance >= 40 Then instance -= 40 returnString &= " & forty " removeAmpersand = True End If If instance >= 30 Then instance -= 30 returnString &= " & thirty " removeAmpersand = True End If If instance >= 20 Then instance -= 20 returnString &= " & twenty " removeAmpersand = True End If If instance = 19 Then returnString &= " & nineteen" instance = 0 End If If instance = 18 Then returnString &= " & eighteen" instance = 0 End If If instance = 17 Then returnString &= " & seventeen" instance = 0 End If If instance = 16 Then returnString &= " & sixteen" instance = 0 End If If instance = 15 Then returnString &= " & fifteen" instance = 0 End If If instance = 14 Then returnString &= " & fourteen" instance = 0 End If If instance = 13 Then returnString &= " & thirteen" instance = 0 End If If instance = 12 Then returnString &= " & twelve" instance = 0 End If If instance = 11 Then returnString &= " & eleven" instance = 0 End If If instance = 10 Then returnString &= " & ten" instance = 0 End If If instance = 9 Then returnString &= If(removeAmpersand, " nine", " & nine") instance = 0 End If If instance = 8 Then returnString &= If(removeAmpersand, " eight", " & eight") instance = 0 End If If instance = 7 Then returnString &= If(removeAmpersand, " seven", " & seven") instance = 0 End If If instance = 6 Then returnString &= If(removeAmpersand, " six", " & six") instance = 0 End If If instance = 5 Then returnString &= If(removeAmpersand, " five", " & five") instance = 0 End If If instance = 4 Then returnString &= If(removeAmpersand, " four", " & four") instance = 0 End If If instance = 3 Then returnString &= If(removeAmpersand, " three", " & three") instance = 0 End If If instance = 2 Then returnString &= If(removeAmpersand, " two", " & two") instance = 0 End If If instance = 1 Then returnString &= If(removeAmpersand, " one", " & one") instance = 0 End If Return returnString.TrimStart(New Char() {" "c, "&"c}).Replace(" ", " ").Trim End Function 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.
-
May 8th, 2011, 06:24 PM
#13
Re: vb2008 extensions
 Originally Posted by minitech
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 8th, 2011, 07:15 PM
#14
Last edited by minitech; May 9th, 2011 at 02:51 PM.
-
May 9th, 2011, 07:59 AM
#15
Re: vb2008 extensions
i haven't tried it but i don't see how 167 lines is trimmed down compared to my 155 lines
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 9th, 2011, 01:02 PM
#16
Re: vb2008 extensions
Well, I added negative numbers, -illions up to sextillions, fractions, decimal points, etc.
-
Jun 14th, 2011, 12:50 PM
#17
Re: vb2008 extensions
here's some extensions for 2d integer arrays:
vb Code:
Module extensions
''' <summary>
''' 2d integer array sum function
''' </summary>
''' <param name="instance"></param>
''' <returns>sum of all elements</returns>
''' <remarks></remarks>
<System.Runtime.CompilerServices.Extension()> _
Public Function sum(ByVal instance(,) As Integer) As Integer
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
End Function
''' <summary>
''' 2d integer array sumColumn function
''' </summary>
''' <param name="instance"></param>
''' <param name="columnIndex"></param>
''' <returns>sum of column</returns>
''' <remarks></remarks>
<System.Runtime.CompilerServices.Extension()> _
Public Function sumColumn(ByVal instance(,) As Integer, ByVal columnIndex As Integer) As Integer
Return Enumerable.Range(0, instance.GetUpperBound(0) + 1).Select(Function(i) instance(i, columnIndex)).Sum
End Function
''' <summary>
''' 2d integer array sumRow function
''' </summary>
''' <param name="instance"></param>
''' <param name="rowIndex"></param>
''' <returns>sum of row</returns>
''' <remarks></remarks>
<System.Runtime.CompilerServices.Extension()> _
Public Function sumRow(ByVal instance(,) As Integer, ByVal rowIndex As Integer) As Integer
Return Enumerable.Range(0, instance.GetUpperBound(1) + 1).Select(Function(i) instance(rowIndex, i)).Sum
End Function
End Module
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 19th, 2011, 05:59 PM
#18
Re: vb2008 extensions
this is a vb2010 extension for reading a csv file into a 2d array:
vb Code:
Module extensions
''' <summary>
''' to2DStringArray
''' </summary>
''' <param name="instance">a 1D string array</param>
''' <returns>a 2D string array</returns>
''' <remarks></remarks>
<System.Runtime.CompilerServices.Extension()> _
Public Function to2DStringArray(ByVal instance As String()) As String(,)
Dim csvArray(instance.GetUpperBound(0), instance(0).Split(","c).GetUpperBound(0)) As String
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)))
Return csvArray
End Function
End Module
you use it like this:
vb Code:
Dim csv(,) As String = IO.File.ReadAllLines("csv.txt").to2DStringArray()
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 6th, 2012, 07:46 PM
#19
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)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 26th, 2013, 03:39 PM
#20
Lively Member
Re: vb2008 extensions
Produces incorrect results when the values are quoted, eg Why not just use the TextFieldParser?
-
Sep 26th, 2013, 03:48 PM
#21
Re: vb2008 extensions
 Originally Posted by AdamPanic2013
Produces incorrect results when the values are quoted, eg Why not just use the TextFieldParser?
which post are you referring to?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 26th, 2013, 04:36 PM
#22
Lively Member
Re: vb2008 extensions
 Originally Posted by .paul.
which post are you referring to?
Post 18
-
Sep 26th, 2013, 05:49 PM
#23
Re: vb2008 extensions
Re: toFullDateString: Wouldn’t something like this work?
Code:
Return Me.Day.OrdinalSuffix() & Me.ToString(" MMMM yyyy")
-
Sep 26th, 2013, 05:55 PM
#24
Re: vb2008 extensions
 Originally Posted by minitech
Re: toFullDateString: Wouldn’t something like this work?
Code:
Return Me.Day.OrdinalSuffix() & Me.ToString(" MMMM yyyy")
I don't follow...?
can you explain?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 27th, 2013, 02:27 PM
#25
Lively Member
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
-
Sep 27th, 2013, 03:05 PM
#26
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.
-
Sep 27th, 2013, 03:22 PM
#27
Re: vb2008 extensions
 Originally Posted by AdamPanic2013
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
 Originally Posted by minitech
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 27th, 2013, 03:40 PM
#28
Re: vb2008 extensions
 Originally Posted by minitech
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???
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 27th, 2013, 04:55 PM
#29
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.
-
Sep 27th, 2013, 05:02 PM
#30
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 27th, 2013, 05:15 PM
#31
Re: vb2008 extensions
 Originally Posted by minitech
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.
 Originally Posted by .paul.
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 28th, 2013, 12:28 PM
#32
Lively Member
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
-
Sep 28th, 2013, 01:38 PM
#33
Re: vb2008 extensions
 Originally Posted by minitech
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 28th, 2013, 02:36 PM
#34
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 21st, 2013, 03:31 PM
#35
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|