DEV Community

Cover image for VB .Net: PasswordFromKeys
vblover programmer
vblover programmer

Posted on

VB .Net: PasswordFromKeys

Method:

    Function PasswordFromKeys() As String
        Dim Key As ConsoleKeyInfo
        Dim Expr As String = ""
        Do
            Key = Console.ReadKey(True)
            Select Case Key.Key
                Case ConsoleKey.Enter
                    Exit Do
                Case ConsoleKey.Escape
                    Return ""
                Case ConsoleKey.Backspace
                    If Expr.Length = 0 Then Continue Do
                    Expr = Expr.Remove(Expr.Length - 1, 1)
                    Console.Write("{0} {0}", Key.KeyChar)
                Case ConsoleKey.NumPad0 To ConsoleKey.NumPad9,
                     ConsoleKey.D0 To ConsoleKey.D9,
                     ConsoleKey.A To ConsoleKey.Z
                    Expr += Key.KeyChar
                    Console.Write("*")
                Case Else
                    Continue Do
            End Select
        Loop
        Return Expr
    End Function
Enter fullscreen mode Exit fullscreen mode

Using:

Console.ForegroundColor = ConsoleColor.White
Console.Write("{0} Type Password: ", vbTab)
Console.ForegroundColor = ConsoleColor.Green
Dim Password As String = PasswordFromKeys()
Console.WriteLine()
Console.ForegroundColor = ConsoleColor.White
Console.Write("{0} Your Password is: ", vbTab)
Console.ForegroundColor = ConsoleColor.Green
Console.WriteLine("{0}", Password)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)