User Tools

Site Tools


k12:k12.2:k12.2.6:start

12.2.6 Form - Persistence

Imagine the following situation: You open a (main) window FMain and from it you open another window F2. Then you edit selected data in window F2. Then you close the F2 window.

What answer would you have to the question: How do you realise the (necessary) data exchange between the window F2 and the (main) window FMain?

Normally, after closing F2, all data related to the form F2, such as text in a TextBox or the values of variables in the class F2, are lost. You can prevent this by setting the property F2.Persistent to 'True' for form F2. This is simple and effective, because you can access the data of the objects in the F2 window at any time - even if the F2 window has been closed.

The following experience of working with the Form.Persistent property can be given:

  • Persistence is used primarily for dialogue windows that are used, for example, to read or enter data.
  • You must set the public property of control elements whose properties and methods are accessed alternately by the two forms FMain and F2 to the value 'True'. Attention: This only works in the form editor of the IDE; at runtime, these changes to the virtual public property of control elements do not succeed!
  • If timers are used for a dialogue form, they only continue to run after the form is closed if the form is persistent! This also applies to other control elements that produce events.
  • Every action with the dialogue triggers form persistence for all objects of a form, even if Persistent is set to 'False'.
  • Practically, all objects of a form are persistent in principle - regardless of the Persistent property. The Persistent property only prevents the properties of objects from being deleted on F2.Close().
  • F2.Hide() has the same deleting effect as F2.Close().
  • It is obviously the case that the functionality of a window F2 whose Persistent property has been set to 'True' can be used immediately (persistently) without having seen the window F2 on the monitor! A background programme in the true sense of the word.
  • The Utility property must not be set to 'True' for the (main) window, because then the F2 window is not displayed!

12.2.6.1 Project

In the (main) window FMain you can change the property F2.Persistent via a CheckBox. It also displays the current values for the range of random numbers from the two control elements (TextBox First, TextBox Last) on F2. To realise this, the Public property has been set to the value 'True' for both SpinBars on F2.

B1
Figure 12.2.6.1.1: (Main) window

In the presented project, the class F2 generates only random numbers, whose range in the window F2 can be changed via two control elements (SpinBar). The F2 window is not displayed when the (main) window is opened.


Figure 12.2.6.1.2: (Main) window with dialogue window F2 open.

If you now change the values for the range of random numbers, the changed values will be displayed in the (main) window at the next timer cycle. After closing F2, the result will not surprise you - the changes have not been applied:


Figure 12.2.6.1.3: (Main) window again with the default values values!

All updated data in F2 is deleted and reset to the default values because the Persistent property of F2 was set to the value 'False' when the (main) window was started. This is changed, as you can see from the highlighted CheckBox:


Figure 12.2.6.1.4: (Main) window with default values

New values for the range of random numbers are now set in the dialogue:


Figure 12.2.6.1.5: (Main) window and dialogue window F2


Figure 12.2.6.1.6: (Main) window

With the adopted, new dialogue values, F2 now only generates random numbers from 7 to 14 and displays them in the (main) window. Attention: Changing the state of the CheckBox does not automatically change the functionality of F2! You can only achieve this by calling F2 again after the value for the CheckBox has been set to 'not selected' and by closing F2:


Figure 12.2.6.1.7: (Main) window

The source code is given for both classes and commented:

[1] ' Gambas class file
[2]
[3] Public Sub Form_Open()
[4]   FMain.Resizable = 'False'
[5] ' FMain.Utility = 'True' ' ERROR!
[6]   Timer1.Delay = 1000
[7]   Timer1.Enabled = 'True'
[8]   Timer1.Trigger()
[9]   F2.Persistent = 'False'
[10] End
[11]
[12] Public Sub btnShowFormP_Click()
[13]   If ckBoxPersistence.Value Then
[14]      F2.Persistent = 'True'
[15]   Else
[16]      F2.Persistent = 'False'
[17]   Endif
[18]   F2.Showmodal()
[19] End
[20]
[21] Public Sub Timer1_Timer()
[22]   txbFirst.Text = F2.sbarFirst.Value
[23]   txbLast.Text = F2.sbarLast.Value
[24]   txlRandomNumber.Text = F2.SetRandomNumber()
[25] End

Comment:

Line 9 ensures that the persistence property of F2 is set to 'False' when the (main) window is opened.The timer (lines 6 to 8) generates the clock in which the random numbers (line 24) are retrieved and displayed as the function value of the SetRandomNumber() function from the Dialog class, and in lines 22 and 23 the default values for the current range of random numbers.

In lines 12 to 19, the persistence property of F2 is set and then the dialogue window F2 modal is displayed.

Source code F2.class:

[1] ' Gambas class file
[2]
[3] Public Sub Form_Open()
[4]   F2.Center()
[5] End
[6]
[7] Public Function SetRandomNumber() As Integer
[8]   Randomize
[9]   Return Rand(sbarFirst.Value, sbarLast.Value)
[10] End
[11]
[12] Public Sub btnClose_Click()
[13]   F2.Close()
[14] End

Comment:

In line 9, the SetRandomNumber() function generates random numbers in the specified range. The Rand(First, Last) function generates numbers of type integer!The random number generator is initialised by Randomize with the current timestamp if the optional parameter is missing.

Download

Project

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k12/k12.2/k12.2.6/start.txt · Last modified: 01.02.2022 (external edit)

Page Tools