Microsoft KB Archive/90043

= Example of a &quot;Password&quot; Style Text Box in VB for MS-DOS =

Article ID: 90043

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic 1.0 Standard Edition
 * 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.



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:

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

Text1.Text = &quot;&quot;

Text2.Text = &quot;&quot;

Label1.Caption = &quot;Input Password:&quot;

Label1.Caption = &quot;Your Password is:&quot;

 Save the form and exit the Forms Designer. You will return to VBDOS.EXE.  At the module level of the form, add the following code: DIM SHARED Password$ CONST NUMCHARS = 9 ' Character limit for password. </li>  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$ = &quot;&quot;             ' Clear Temp variable. Text2.Text = Password$ ' Display password. Text1.Text = &quot;&quot;        ' Clear password input box. Password$ = &quot;&quot;         ' 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                       </li> Save your work.</li> From the Run menu, choose Start.</li> In the first box enter a password; an asterisk (*) will be displayed for each character you enter.</li> Press ENTER.</li></ol>

The first text box will clear, and the second will contain your password.

Additional query words: VBmsdos

Keywords: KB90043

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.