B4J Question Supress events if gui is set within programm code

specci48

Well-Known Member
Licensed User
Is it possible to differentiate between user clicked events and events which are fired because of program based setting?

E.g. there is a Checkbox with
B4X:
Sub Checkbox_CheckedChange(Checked As Boolean)
  ' ... some code
End Sub
If the user checks the Checkbox with the mouse, the CheckedChange event and the sub Checkbox_CheckedChange is called.

If I set
B4X:
Checkbox.Checked = True
within my program, the sub Checkbox_CheckedChange ist called, too.

Is there a way to differ between both calls or better prevent the sub Checkbox_CheckedChange to be called if set within program code?
 

Erel

Administrator
Staff member
Licensed User
Check this example:
B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Private chk1 As CheckBox
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.Show
   chk1.Initialize("chk1")
   MainForm.RootPane.AddNode(chk1, 10, 10,50, 50)
End Sub

Sub MainForm_MouseClicked (EventData As MouseEvent)
   SetCheckState(chk1, Not(chk1.Checked))
End Sub

Sub SetCheckState(chk As CheckBox, state As Boolean)
   If IsNumber(chk.Tag) = False Then chk.Tag = 0
   chk.Tag = chk.Tag + 1
   If chk.Checked <> state Then chk.Checked = state
End Sub

Sub chk1_CheckedChange(Checked As Boolean)
   If IsNumber(chk1.Tag) AND chk1.Tag > 0 Then
     chk1.Tag = chk1.Tag - 1
     Return
   End If
   Log("User changed state: " & Checked)
End Sub
Click on the form to change the value programmatically.
 
Top