B4J Question Cancel an Action before it executes

canalrun

Well-Known Member
Licensed User
Longtime User
Hello,
In B4J is there a way to prevent an Action from occurring if I don't want it to occur?

For example, before I respond to a ListView item click, a button press, or the application close button press, I want to check if there are any unsaved changes.

I'm using the "_Action" event to respond to item clicks, button presses, etc.

Is there a way know that the action is about to happen and stop it before it happens?

For example, when someone clicks on an item in the ListView, I want to first check if there are unsaved changes and be able to prevent things like the selection index changing and _Action event being called.

... Maybe there is an event that happens before _Action where if you return False the action is canceled?

Thanks,
Barry.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can cancel the app closing with:
B4X:
Sub MainForm_CloseRequest (EventData As Event)
   If fx.Msgbox2(MainForm, "close?", "", "yes", "", "no", fx.MSGBOX_CONFIRMATION) <> fx.DialogResponse.POSITIVE Then
     EventData.Consume
   End If
End Sub

About the other actions, you can cancel the action in the action event:
B4X:
Sub Button1_Action
   If SomeCondition Then Return
   'format c:
End Sub
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Thanks. CloseRequest will definitely help.

When somebody clicks on a ListView item, the SelectedIndex is set.

I thought this would be a problem, but it may not be. Hopefully I can just return SelectedIndex to its previous value.

Barry.
 
Upvote 0
Top