In this chapter, a project presents, among other things, a dialogue that follows the descriptions for user-defined dialogues explained in detail in chapter → 12.4. In this project, the special _call(..) method is used to call the dialogue window. It allows an object to be used syntactically like a function. By calling the _call(..) method, the self-designed dialogue window can display itself modally! Good to know: In the optional arguments of the _call(…) method you can pass data to the dialogue window and in the return value of the method you can read data from the dialogue as a function value, save it and process it further.
The special feature of the project is the password-protected access to a (main) programme via a dialogue window to request a password. The login_password file contains the required password, which is stored encrypted in the file. The password file is generated in a separate project:
Figure 12.2.4.1: Generate password
sHash = Digest["sha512"](txbPassword.Text) File.Save(Application.Path &/ "login_password", sHash)
Login window as dialogue
Already when the main programme is opened, the password is read from the password file and assigned to the variable hHash. Even before the main programme is displayed, the login window is opened as a dialogue and the user is prompted to enter the password:
' The login window (dialogue) is opened *modally* before the main window: fLoginDialog = New FLogin aDialogData = fLoginDialog("Enter the login password:")
Figure 12.2.4.2: Create password
If the dialogue window is closed, it can be determined whether the user has cancelled the dialogue or entered an incorrect password:
' Dialog auswerten If Not aDialogData Then ' Dialogue abort? Me.Close() ' Close main window *without* comment Return Endif ' Passwort-Check If Digest["sha512"](aDialogData["Password"]) <> sHash Then ' Error? End programme with comment. Message.Title = "Password Error ..." Message.Error("<center>Login failed!<hr>The main programme is terminated.</center>") Me.Close() Return Endif
Figure 12.2.4.3: Password error
In both cases, the main programme is terminated and only in the case of the incorrect password is a comment output.
If the stored encrypted password in the password file matches the password entered in the dialogue window, the main programme is displayed:
Figure 12.2.4.4: Main program window
The source code for the login dialogue is given in full:
' Gambas class file Public Function _call(sMessage As String) As Collection lblMessage.Text = sMessage ' Returns when one of the buttons has been clicked! ' The return value is indicated in the Me.Close() call and ' shows whether it was cancelled or not. txbPassword.SetFocus() If Me.ShowModal() = 0 Then Return Null Return ["Name": User.Name, "Password": txbPassword.Text] ' Me.ShowModal() = 1 End ' Function _call(...) Public Sub btnOK_Click() If Not txbPassword.Text Then Message.Title = "Password error ..." Message.Warning("No password was entered.") txbPassword.SetFocus() Return Endif Me.Close(1) End ' btnOK_Click() Public Sub btnCancel_Click() ' 0 is also returned when closing by the cross in the window bar; ' this corresponds to an abort of the dialogue by the user. Message.Warning("The login was cancelled ....") Me.Close(0) End Public Sub txbPassword_Activate() btnOK_Click() End ' txbPassword_Activate()
In the download area you will find the two projects mentioned above.
Project