Password Stars
Windows Forms apps make it too easy to create something without writing much (if any) code. For example, to have a user enter a password displaying only asterisks is simply done by changing the textbox property. But isn't it more satisfying to actually program your own code for it, as in this Console application?
Module
Module1Sub Main()Dim ch, wordAsStringword =
""Console.Write(
"Enter your password: ")Doch = Console.ReadKey(
True).KeyChar'get character without displayIf ch = Chr(8)And word.Length > 0Then'backspace to deleteConsole.CursorLeft = Console.CursorLeft - 1
'move back one columnConsole.Write(
" ")'erase last starConsole.CursorLeft = Console.CursorLeft - 1
word = word.Substring(0, word.Length - 1)
'delete last characterElseIf ch > Chr(32)Then'no spaces or control keysConsole.Write(
"*")'print starword = word & ch
'concatenate character to passwordEndIfLoopUntil ch = Chr(13)'Enter key to stopConsole.WriteLine(vbCrLf)
Console.WriteLine(
" Your password was: {0}", word)Console.WriteLine()
EndSubEnd
Module
I love trying to figure out how things work, and making a working password field is a good example of this.
In fact, the second program I ever wrote was a masked password entry program, back on the old BBC Microcomputer
(featuring 3 colors: green, dark green, and light green). Those were the days...
Keep experimenting with things and you'll find your knowledge of programming (not necessarily your knowledge of the language) will increase exponentially!
I actually wrote this program in QBasic several years ago. I've been teaching an introductory programming course for several years using QBasic, in addition to a follow-up course using VB and C#. With QBasic, you have no shortcuts and really need to understand the logic and programming structures, which is the whole purpose of the course. This semester, we switched to VB 2005 for the introductory course as well, but using mainly Console applications.
I had prepared hundreds of original little QBasic demo programs to accompany each of my lectures, all of which can fit on a single floppy disk. I had printed out groups of these little demos on each page for distribution to my students. It's been a challenge to translate them into fewer, but larger yet cohesive VB demo projects, each of which will print out on a single page for distribution. Preparing hundreds of little VB projects would be prohibitive because of the size of each .NET project, and it would require about 2 dozen (or more) floppies. Even so, this semester I am using a USB flash disk instead of floppy disks, and so are my students.
I was happy to see the new Console features that were added to the 2.0 framework of VS, that made it possible to emulate some of the code previously used in QBasic. So as you see, I've been experimenting with pure code for quite a while. I still prefer QB for working out the logic of a program before translating it into VB, without the hassle of the GUI or verbose syntax.
By the way, I revised the code in the above post and removed the 2 CursorLeft lines by using vbBack instead, as shown below. This little sample is part of a larger demo program I wrote to illustrate a few string manipulation techniques:
Module Module1
Sub Main()
Dim ch, word As String
word =
""Console.Write(
"Enter your password: ")Doch = Console.ReadKey(
True).KeyChar 'get character without displayIf ch = Chr(8) And word.Length > 0 Then 'backspace to deleteConsole.Write(vbBack &
" " & vbBack) 'erase last starword = word.Substring(0, word.Length - 1)
'delete last characterElseIf ch > Chr(32) Then 'no spaces or control keysConsole.Write(
"*") 'print starword = word & ch
'concatenate character to passwordEnd IfLoop Until ch = Chr(13) 'Enter key to stopConsole.WriteLine(vbCrLf)
Console.WriteLine(
" Your password was: {0}", word)Console.WriteLine()
End SubEnd
Moduleusing System;
using System.Collections.Generic;
using System.Text;
namespace PasswordStars_CS
{
class Program
{
static void Main(string[] args)
{
char ch = (char)(0);
string word = "";
Console.Write("Enter your password: ");
do
{
ch = Console.ReadKey(true).KeyChar; //get character without display
if (ch == (char)(8) & word.Length > 0) //backspace to delete
{
Console.Write("\b \b"); //erase last star
word = word.Substring(0, word.Length - 1); //delete last character
}
else if (ch.CompareTo((char)(32)) > 0) //no spaces or control keys
{
Console.Write("*"); //print star
word = word + ch; //concatenate character to password
}
} while (ch != (char)(13)); //Enter key to stop
Console.WriteLine("\n\n Your password was: {0}\n", word);
}
}
}
Slightly altered version of above code:
using System;
using System.Collections.Generic;
using System.Text;
namespace PasswordStars_CS
{
class Program
{
static void Main(string[] args)
{
string word = "";
char ch = '\0';
Console.Write("Enter your password: ");
do
{
ch = Console.ReadKey(true).KeyChar; //get character without display
if (ch == '\b' & word.Length > 0) //backspace to delete
{
Console.Write("\b \b"); //erase last star
word = word.Substring(0, word.Length - 1); //delete last character
}
else if (ch > (char)(32)) //no spaces or control keys
{
Console.Write("*"); //print star
word = word + ch; //concatenate character to password
}
} while (ch != '\r'); //Enter key to stop
Console.WriteLine("\n\n Your password was: {0}\n", word);
}
}
}