12.4.4.1 Own Dialogues - Project 1

In the first example, a dialogue is developed that is called up before the main program and is used to enter two pieces of data - a freely selectable name and a password. When planning your own dialogue, questions to be answered include the following, although the order does not imply any ranking:

In relation to the 1st project, here are the answers:

B1
Figure 12.4.4.1.1: Dialogue box (IDE)

With these preliminaries, the development of a custom dialogue can be tackled.

In essence, one defines a dialogue box → Figure 12.4.4.1.1 and calls it using the special _call(..)-method in the main program. For information on this method, see the Gambas documentation → http://gambaswiki.org/wiki/lang/special/call. In the optional arguments of the _call(…) method you can pass data to the dialogue and read data from the dialogue in the return value of the method (function value). By calling the _call(..) method, the dialogue box can display itself modally! Now you can edit the data in the dialogue. If the dialogue box is closed, the ShowModal() call - this happened automatically - returns to the _call() call and you can read data from the dialogue.

This all takes place in the Open event of the main form in the first example - before this form is displayed! The _call() method allows you to use an object syntactically like a function. Use the _call(..) method whenever a form (or a class in general) has a very specific task that it can do itself, without needing event handlers in other classes, for example.

The following two source texts for example 1 show the implementation of an own dialogue in Gambas:

Source code (main) program (FMain.class):

[1] ' Gambas class file
[2]
[3] Public Sub Form_Open()
[4]   Dim cData As Collection
[5]   Dim sDialogTitel As String
[6]   Dim frmLogin As New FLogin
[7]
[8]   FMain.Center
[9]   FMain.Resizable = False
[10]   sDialogTitel = "Enter the data!"
[11]
[12] ' Before the main window is displayed, the login window is automatically opened 'modally'.
[13]   cData = frmLogin(sDialogTitel)
[14]   If Not cData Then ' Cancel?
[15]   Me.Close() ' Exit the main program without a comment.
[16]      Return
[17]   Endif
[18]
[19]   If cData["password"] <> "mgA+" Then ' Failed login attempt? End with comment.
[20]      Message.Title = "Error in the dialogue box".
[21]      Message.Error("Login failed!")
[22]      Me.Close()
[23]      Return
[24]   Else
[25]    ' Otherwise start main program ...
[26]     lblGreet.Text = Subst$("So you are &1. Good to know!", cData["name"])
[27]   Endif
[28]
[29] End ' Form_Open()

Comment:

Source code dialogue (FLogin.class):

[1] ' Gambas class file
[2]
[3] Public Sub _call(sTitel As String) As Collection
[4]   Me.Text = sTitel
[5]
[6]   If Me.ShowModal() = 0 Then
[7]      Return Null
[8]   Else
[9]      Return ["name": txtName.Text, "password": txtPassword.Text]
[10]   Endif
[11]
[12] End ' _call(sTitel As String)
[13]
[14] Public Sub btnCancel_Click()
[15]   Me.Close(0)
[16] End ' btnCancel_Click()
[17]
[18] Public Sub btnOK_Click()
[19]   Me.Close(1)
[20] End ' btnOK_Click()
[21]
[22] Public Sub txtName_Activate()
[23]   txtPassword.SetFocus()
[24] End ' txtName_Activate()
[25]
[26] Public Sub txtPassword_Activate()
[27]   btnOK_Click()
[28] End ' txtPassword_Activate()

Comment:

Notes:

The results in the first example are not spectacular - but exactly those that were to be expected.The dialogue is displayed as a preliminary program and you are prompted to enter the required data:

B2
Figure 12.4.4.1.2: Dialogue Box - Data Entry

If the password is incorrect - stored in the return value of the dialogue cData[“password”] - you will see this message and then nothing more … .

B3
Figure 12.4.4.1.3: Error message

If the login was successful, the main program is displayed. The name entered in the dialogue (cData[“name”]) is used in the comment:

B4
Figure 12.4.4.1.4: The main program is displayed …

Download

Project

Download