VB Tutorial 9 - Passwords & Splash
 
In our last tutorials we learned to use components and textboxes as part of a database exercise. In this tutorial we return to the use of textboxes setting up a password and a splash screen for our application. In this exercise we read an encrypted password from an setup.ini file. Then when the user enters the correct password, we start up a splash screen. Two advantages of using a splash screen is that it not only allows you to advertise your program and highlight its wares; but also you have time to initialize a programs global variables, reference tables and database connections. This may not seem important now - but trust me a VB program with several database connections and long listboxes to initialize can take 10-25 seconds to load - a splash screen is much better than just staring at the password dialog.

Here is the trick. Do 1/3 of the initializing on the Form_Load of the password procedure. Then if the password is okay; do the next 1/3 of the initializing on the Form_Load of the splash screen. Then do the final initialization on start-up of your final main form. But first, lets see how to code a password screen.

Figure 1 - Password Dialog

Passing Words

Add two textboxes to a new project form, call it "passby". The labels for the two textboxes are as shown in Figure 1. The first textbox does not require any special setting but the password textbox does. Its PasswordChar property is set to the character "*". This causes the textbox to be filled with asterisks for each character typed. But the value of passwd.Text seen by the program is the password typed in by the user.
The second trick we have used is to make sure whenever the password textbox gains focus, the text in the box is automatically selected with the following code:

REM 'This code highlights/selects the text when you enter
Screen.ActiveControl.SelStart = 0
ii = Len(Screen.ActiveControl.Text)
Screen.ActiveControl.SelLength = ii


Now readers of VB Tutorial seven will find portions of this code very familiar. Screen.ActiveControl refers to the control which currently has focus or is active. This of course is the textbox "passwd". By setting the active control's SelStart property to 0 and its SelLength property equal to the length of the text in the box - this selects or highlights the text in the password immediately on entry. This means the user does not have to be sure to erase the contents before entering the password - just a convenience.

The next trick we have is to recognize if the user types the Enter key when he is done entering the password. VB does not recognize entry of the Enter key - you have to check for it with the following code:

Private Sub passwd_KeyPress(KeyAscii As Integer)
' detect when user presses "Enter" key and if so validate password
If KeyAscii = 13 Then
  '   this is the call on the routine that does the password checking
  Call passwd_Validate(False)
End If
End Sub

This subroutine is called every time a letter is typed in the password textbox. It is look for one KeyAscii code, 13, which is the value of the Enter key. When that is pressed then control is immediately passed to the password validation procedure. Of course we also call the password validation after the Okay button is clicked or when the password textbox is exited.

The password validation procedure is fairly simple. On Form_Load we have already gotten the decrypted password from the setups file. So now all we need to do is check if the entered password matches the decrypted password. If it does not we issue a warning message asking the user to re-enter the password. We could keep a count of the tries and then exit from the password procedure altogether.

However, if the password is valid then we call the splash screen routine as follows:

' We are in call the splash screen
passplash.Show

where passplash is the name of the Splash screen form. Its here where the last 2 tricks are performed in our program. First, we need to hide the password form from the user - because its is the calling window we cannot just destroy/kill it but have to hide it with the code line PassForm.hide. The second trick is to do any leftover initialization required. And with these tricks we have a solid set of program start up code.
Summary

In this tutorial we have shown some tricks associated with using text boxes and getting passwords and the use of splash screens. We have again used VB's predefined objects to good use. Finally we take advantage of some of the many procedures and events associated with textboxes like Keypress or Validate.

Resources:

The book Beginning VB6/Wrox covers VB textboxes on pages 73-94; VB6 Black Book/Coriolis touches on textboxes on pages 197-221.
 
Top of Page  Tutorials Home