Example of a "Password" Style Text Box in VB for MS-DOS (90043)



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition for Windows 1.0
  • Microsoft Visual Basic for MS-DOS

This article was previously published under Q90043

SUMMARY

In Microsoft Visual Basic for MS-DOS and Microsoft Visual Basic for Windows, there is no intrinsic setting for a password-style edit control. However, code can be generated to perform this type of functionality using the existing edit control.

MORE INFORMATION

Example

The following example will demonstrate how to set up a text box to accept a user's password as input. It will display an asterisk (*) for each character entered.

To try this example in VBDOS.EXE:

  1. Start Microsoft Visual Basic for MS-DOS.
  2. From the Edit menu, choose New Form, and name the form Form1.FRM.
  3. Add two text boxes and two labels. The CtrlNames will be the default (Text1, Text2, Label1, and Label2).
  4. Place the labels to the left of each text box.
  5. Set the following properties for the controls listed below:

    Text1.Text = ""
    Text2.Text = ""
    Label1.Caption = "Input Password:"
    Label1.Caption = "Your Password is:"

  6. Save the form and exit the Forms Designer. You will return to VBDOS.EXE.
  7. At the module level of the form, add the following code:
            DIM SHARED Password$
            CONST NUMCHARS = 9  ' Character limit for password.
  8. In the Text1_KeyPress event, add the following code:
       Sub Text1_KeyPress (KeyAscii AS INTEGER)
           STATIC Temp$    ' Collects characters each time key is pressed.
           IF KeyAscii = 13 THEN       ' ENTER key pressed.
               Password$ = Temp$       ' Assign Password values. collected.
               Temp$ = ""              ' Clear Temp variable.
               Text2.Text = Password$  ' Display password.
               Text1.Text = ""         ' Clear password input box.
               Password$ = ""          ' Clear password when done.
               KeyAscii = 0            ' Prevents Beep after ENTER Key.
           ELSEIF KeyAscii = 8 THEN    ' Backspace key pressed.
               Temp$ = LEFT$(Temp$, LEN(Temp$) - 1) ' Remove one char.
           ELSEIF LEN(Temp$) = NUMCHARS THEN      ' Limit size of password.
               KeyAscii = 0
               BEEP                    ' Signal user that field is full.
           ELSE
               Temp$ = Temp$ + CHR$(KeyAscii)     ' Add a character.
               KeyAscii = 42           ' Print a '*' instead of character.
           END IF
    						
  9. Save your work.
  10. From the Run menu, choose Start.
  11. In the first box enter a password; an asterisk (*) will be displayed for each character you enter.
  12. Press ENTER.
The first text box will clear, and the second will contain your password.

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB90043 kbAudDeveloper