firing a UI event programmatically?

Dave O

Well-Known Member
Licensed User
Longtime User
I have several imageViews that share a click event subroutine.

Is it possible for me to call that event programmatically for a given imageView instance?

That is, code something like "imageView1.click", so that the sub gets called and the Sender object is set to that view instance.

Background - I have a palette of tools, and I'd like to select one of them by default when the app starts. So I figured I would "click" the tool I want in the activity.create sub.

Better ideas also welcome for this particular problem, but also interested in the more general question of triggering events programmatically.

Thanks!
 

cammel8

Member
Licensed User
Longtime User
I have several imageViews that share a click event subroutine.

Is it possible for me to call that event programmatically for a given imageView instance?

That is, code something like "imageView1.click", so that the sub gets called and the Sender object is set to that view instance.

Background - I have a palette of tools, and I'd like to select one of them by default when the app starts. So I figured I would "click" the tool I want in the activity.create sub.

Better ideas also welcome for this particular problem, but also interested in the more general question of triggering events programmatically.

Thanks!

just call it where you need it.

Say i have a button_click event that runs a toastmessage. i want to run the message attached to the click event somewhere in say the activity create area. I type the name of the sub there and it will act as if it clicked and that sub will run.

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
 dim button1 as button
End Sub

Sub Activity_Create(FirstTime As Boolean)
button1_click
End Sub

Sub Activity_Resume

End SubSub Activity_Pause (UserClosed As Boolean)

End Sub

sub Button1_click
 toastmessageshow("Hello",true)
end sub

when a person clicks something it runs the sub just the same as if you call it
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
That was my initial thought, but I don't think that would pass the right "Sender" object, would it?
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
Just try and see what happens. If it dont work you can always revert but it should work. I have done it multiple times without problem.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
That was my initial thought, but I don't think that would pass the right "Sender" object, would it?

How can it set the Sender object when you are firing it programatically? There is no sender.
You will have to set a global variable to check whether the event was fired programatically or normally, and then another variable to check who the sender was when you call it programaticaly.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

Split the Sub into two Subs.

Say your original Sub is like this:

B4X:
Sub ImageView1_Click
Dim iv As ImageView
iv=Sender
' do something here
End Sub

Two Subs:

B4X:
Sub ImageView1_Click
Dim iv As ImageView
iv=Sender
DoSomething(iv)
End Sub

Sub DoSomething(ImageView2 As ImageView)
' do something here
End Sub

And you can now programmatically 'do something' anywhere in the code:

B4X:
DoSomething(AnImageView)

Martin.
 
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Martin, thanks for that - a reasonable workaround for cases where you need the Sender.

In some other languages I've used, UI events are also object methods, so you can just write "imageView1.click" - no workarounds required. I'll leave that for the B4A wish list. ;)
 
Upvote 0

adamioan

Member
Licensed User
Longtime User
You can achieve just by setting a global variable
Dim TheSenderTag as String
and fill it before you call the click event
TheSenderTag= ( the info you need)
ImageView1_click

In the click event:
If TheSenderTag="" then
TheTag = Sender.tag ' Get the tag from the sender object
Else
TheTag=TheSenderTag ' Get the tag from the TheSenderTag variable
TheSenderTag="" ' Empty the variable value
End if
 
Upvote 0
Top