Buttons on a panel

IamBot

Member
Licensed User
Longtime User
I can't make buttons work on a panel. They appear as they should and fully clickable, but clicking does not trigger "Sub btnLan_Click". See code below:

PHP:
Sub Activity_Create(FirstTime As Boolean)

 Dim p As Panel
 Dim btnLan As Button

 p.Initialize("")
 btnLan.Initialize("")

 Dim cd As CustomDialog

 cd.AddView(p,2dip,0,90%x-4dip,220dip)

 p.AddView(btnLan,150dip,30dip,50dip,40dip)
 
 cd.Show("XXXXXX","Ok","Cancel","",Null)
 
End Sub

Sub btnLan_Click

   Msgbox("Sub Entered", "It worked!!")

End Sub

Please assist.
 

mc73

Well-Known Member
Licensed User
Longtime User
Insert into button initialization, this:
B4X:
btnLan.initialize("btnLan")
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
This works:
B4X:
Sub Globals

    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
            
    Dim MyPanel As Panel
    Dim btnLan As Button

End Sub

Sub Activity_Create(FirstTime As Boolean)

    MyPanel.Initialize("")
    btnLan.Initialize("btnLan")
    btnLan.Text = "Click Me"
            
    Activity.AddView(MyPanel, 0dip, 0dip, 100%x, 100%y)
            
    MyPanel.AddView(btnLan, 0dip, 0dip, 100dip, 50dip)


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause(UserClosed As Boolean)
            
End Sub



Sub btnLan_Click
   
    Msgbox("Boo!!", "")
   
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
This works:
B4X:
Sub Globals

    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
                
    Dim MyPanel As Panel
    Dim btnLan As Button

End Sub

Sub Activity_Create(FirstTime As Boolean)

    MyPanel.Initialize("")
    btnLan.Initialize("btnLan")
    btnLan.Text = "Click Me"
                
    Activity.AddView(MyPanel, 0dip, 0dip, 100%x, 100%y)
                
    MyPanel.AddView(btnLan, 0dip, 0dip, 100dip, 50dip)


End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause(UserClosed As Boolean)
                
End Sub



Sub btnLan_Click
    
    Msgbox("Boo!!", "")
    
End Sub

Sure, this works, since it is not a modal form, and actually this is what I would personally choose to do, instead of using a custom dialog, in this particular case (even though i would then have to handle events elsewhere on the form).
Anyway, I've read some things, I think that if you insist on using custom dialog, you should show it outside of the activity_create, and then forget about msgbox, since this is also a modal form.
Here's a sample:
B4X:
'Activity module
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 showDialogButton As Button 
End Sub

Sub Activity_Create(FirstTime As Boolean)
showDialogButton.Initialize ("showDialogButton")
showDialogButton.Text="show Custom Dialog"
Activity.AddView (showDialogButton,0,0,50%x,10%y)

End Sub

Sub btnLan_Click
Log("clicked")
  

End Sub  

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub pnl_touch(Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event
    Return True
End Sub
Sub showDialogButton_click
 Dim p As Panel
 Dim btnLan As Button
 Dim cd As CustomDialog

p.Initialize("pnl")
 btnLan.Initialize("btnlan")
btnLan.Text ="test"
 p.AddView(btnLan,150dip,30dip,50dip,40dip)

 cd.AddView(p,2dip,0,90%x-4dip,220dip)

 cd.show("XXXXXX","Ok","Cancel","",Null)
 
End Sub

You might also want to check this thread: http://www.b4x.com/forum/basic4android-updates-questions/18839-customdialog-events.html#post108206
 
Last edited:
Upvote 0

IamBot

Member
Licensed User
Longtime User
Thanks guys!

Didn't expect it to be this complicated just because of the use of a CustomDialog. I will look into your examples and see how I can change my program.

Cheers!
 
Upvote 0
Top