Friend Module Test
Friend Num As Integer = -0I
Friend Sub Main()
Console.Title = "Introduciendo valores... By Elektro"
Console.WriteLine()
Num = ReadNumber(Of Integer)(
MaxValue:=Integer.MaxValue,
Mask:="."c,
CommentFormat:="[+] Introduce un valor {0} :",
IndicatorFormat:=" >> ",
DataTypeFormat:
=New Dictionary(Of Type,
String) From
{
{GetType(Integer),
String.Format("entero (Int32 Max: {0})", CStr(Integer.MaxValue))}
})
Console.WriteLine(Environment.NewLine)
Console.WriteLine(String.Format("Valor {0}: {1}", Num.GetType.Name, CStr(Num)))
Console.ReadKey()
Environment.Exit(0)
End Sub
' By Elektro
'
''' <summary>
''' Reads the console input to wait for an specific numeric value.
''' </summary>
''' <typeparam name="T"></typeparam>
''' <param name="MaxValue">Indicates the maximum value to expect.</param>
''' <param name="Mask">Indicates the character mask.</param>
''' <param name="CommentFormat">Indicates a comment string format.</param>
''' <param name="IndicatorFormat">Indicates a value indicator string format.</param>
''' <param name="DataTypeFormat">Indicates a data type string format.</param>
Friend Function ReadNumber(Of T)(Optional ByVal MaxValue As Object = -0S,
Optional ByVal Mask As Char = "",
Optional ByVal CommentFormat As String = "",
Optional ByVal IndicatorFormat As String = "",
Optional ByVal DataTypeFormat
As Dictionary(Of Type,
String) = Nothing) As T
' A temporal string that stores the value.
Dim TmpString As String = String.Empty
' Stores the current typed character.
Dim CurrentKey As New ConsoleKeyInfo("", ConsoleKey.NoName, False, False, False)
' Retrieve the numeric object Type.
Dim DataType As Type = GetType(T)
' Retrieve the Type name.
Dim ValueFormat As String = DataType.Name
' Retrieve the Type converter.
Dim Converter As System.ComponentModel.TypeConverter =
System.ComponentModel.TypeDescriptor.GetConverter(DataType)
' Set the maximum number value.
If Not CBool(MaxValue) Then
MaxValue = DataType.GetField("MaxValue").GetValue(Nothing)
End If
' Set the maximum number length.
Dim MaxValueLength As Integer = CStr(MaxValue).Length
' Set the indicator length.
Dim IndicatorLength As Integer = IndicatorFormat.Length
' Set the datatype name format.
If DataTypeFormat IsNot Nothing Then
ValueFormat = DataTypeFormat(DataType)
End If
' Write the comment.
If Not String.IsNullOrEmpty(CommentFormat) Then
Console.WriteLine(String.Format(CommentFormat, ValueFormat))
Console.WriteLine()
End If
' Write the indicator.
If Not String.IsNullOrEmpty(IndicatorFormat) Then
Console.Write(IndicatorFormat)
End If
' Write the value mask.
For X As Integer = 0 To MaxValueLength - 1
Console.Write(Mask)
Next
' Set the cursor at the start of the mask.
Console.SetCursorPosition(Console.CursorLeft - MaxValueLength, Console.CursorTop)
' Ready to parse characters!
Do
CurrentKey = Console.ReadKey(True)
Select Case CurrentKey.Key
Case ConsoleKey.Enter ' Accept the input.
Exit Do
Case ConsoleKey.Backspace ' Delete the last written character.
If Not String.IsNullOrEmpty(TmpString) Then
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop)
Console.Write(Mask)
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop)
TmpString = TmpString.ToString.Substring(0, TmpString.ToString.Length - 1)
End If
Case ConsoleKey.LeftArrow ' Move 1 cell to Left.
' Not implemented yet (Too much work deleting character in the current cursor position).
' Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop)
Case ConsoleKey.RightArrow ' Move 1 cell to Right.
' Not implemented yet (Too much work deleting character in the current cursor position).
' Console.SetCursorPosition(Console.CursorLeft + 1, Console.CursorTop)
Case Else ' Another key
Dim NextValue As String = If(Not String.IsNullOrEmpty(TmpString),
TmpString.ToString & CurrentKey.KeyChar,
CurrentKey.KeyChar)
' If value is valid and also does not exceed the maximum value then...
If Converter.IsValid(NextValue) _
AndAlso Not NextValue > MaxValue _
AndAlso Not NextValue.Length > MaxValueLength Then
TmpString = NextValue
Console.Write(CurrentKey.KeyChar)
End If
End Select
Loop
If Not String.IsNullOrEmpty(TmpString) Then ' Return the value.
Return Converter.ConvertFromString(TmpString)
Else
Return Nothing
End If
End Function
End Module