Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

PasswordChar

I shall be as secret as the grave.
—Miguel de Cervantes, Don Quixote de la Mancha

This property makes a task that used to be hard really easy. It lets you specify that a text box should display a particular character instead of the character input by the user. Use it to hide passwords while they’re being input.

Usage

txtTextbox.PasswordChar = cChar
cChar = txtTextbox.PasswordChar

Using PasswordChar doesn’t affect the Value of the text box. It simply hides it from the user. You’ll probably want to combine PasswordChar with an InputMask to limit input to a fixed number of characters.

When you enter a value for PasswordChar in the Property Sheet, do not surround it with quotes. Just type the desired character. Otherwise, you’ll end up with a quote as your PasswordChar.

A number of characters can’t be used for PasswordChar. Assigning any of the prohibited characters sets PasswordChar to the empty string, which results in no masking of input.

Valid PasswordChars are CHR(33) – CHR(126), CHR(130) – CHR(140), CHR(145) – CHR(156), CHR(159), and CHR(161) – CHR(255). This includes all the letters and numbers and lots of punctuation marks. In some fonts, some of the valid characters may be invisible, which leaves the text box empty—not a good choice. In fact, most of the time, the best choice is to use the asterisk “*” (CHR(42)), which is something of a Windows standard for passwords.

You can change PasswordChar dynamically and characters already typed change to the new value. You might use this ability for some cool visual tricks.

Example

* Assume there's a variable cPassword
* containing the password for the current user.
*
* Create a form with a text box and a button
* Using the property sheet, set the text box as follows:
*    PasswordChar = "*" && asterisks
*    InputMask = "AAAA"   && four alphabetic chars
*    Format = "K"         && select all on entry
* Set the button as follows:
*    Caption = "Check"
* In the Click method of the button, put:
IF ThisForm.Text1.Value == cPassword
   * They pass - close the form
   RELEASE ThisForm
ELSE
   * No good - tell the user
   WAIT WINDOW "Invalid Password. Try again"
   ThisForm.Text1.SetFocus
ENDIF
*
* Save the form and run it. When you type in a bad
* password and click Check, you get the message.
* When you type in the right password, the form
* closes.

Of course, in an application, you’d want to make this a lot more foolproof, and that can be tough, ‘cause fools are so darned clever. You’d want a Cancel button to let the user give up. You’d keep the form from being closed except by a valid password or by Cancel. You’d probably have a limit on the number of tries and so forth. The example, though, gives you the basic structure for this kind of test.

See Also

Chr(), InputMask, TextBox