can someone explain this ?

melamoud

Active Member
Licensed User
Longtime User
hi, I wrote some code and I do not understand why some of the buttons does not throw an event when clicked

when press the buttons in the array nothing happen, when pressing the panel (empty space) a text edit and button will appear and when pressing that button it will work
:sign0163:


B4X:
#Region Module Attributes
   #FullScreen: True
   #IncludeTitle: True
   #ApplicationLabel: Covert Communication
   #VersionCode: 1
   #VersionName: 1.0
   #SupportedOrientations: unspecified
   #CanInstallToExternalStorage: True
#End Region

'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   'Dim pws As PhoneWakeState


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 pp As Panel

End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   test_UI

End Sub

Sub Activity_Resume
   'pws.KeepAlive(False)   
   'test_UI
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   'pws.ReleaseKeepAlive ' should I do this here, or prevent app from exit (unless press back twice or something like that)
   ' what happne when usr press home (in the above scenario)
End Sub


Sub test_UI 
   Dim p As Panel
   p.Initialize("p")
   Dim btn(50) As Button
   If Not (p.IsInitialized) Then
      Log ("cant init p")
   Else
      Log ("p is init already, add button")
      For i = 0 To btn.Length -1
         If (1 = 0) Then
            btn(i).Initialize("btn pressed0")
         Else
            btn(i).Initialize("btn pressed")
         End If
         btn(i).Text = i 
         btn(i).Tag = i
         Dim w As Int = 65
         Dim h As Int = 55
         Dim x As Int = (i*(w+5)) Mod (100%x-w)
         Dim y As Int = (Floor((i*(w+5)) / (100%x-w)) * (h+5))         
         Log ("adding btn " & i & " at " & x & "," & y)
         p.AddView(btn(i),x,y,w,h)
      Next
      Activity.AddView(p,0,0,100%x,100%y)
   End If

End Sub


Sub p_Click
   Log ("Panel was pressed!")
   pp.Initialize("")
   Dim edt As EditText
   edt.Initialize("")
   edt.Text = ""
   pp.AddView(edt,100%x/2-100,100%y/2-70,200,70)
   
   Dim btn As Button
   btn.Initialize("ok")
   btn.Text = "ok"
   pp.AddView(btn,100%x/2-100,100,100,60)
   
   Activity.AddView(pp,0,0,100%x,100%y)
   
End Sub

Sub ok_Click
   Log ("ok button was pressed")
   pp.RemoveView
End Sub


Sub btn_pressed0_Click
   Log ("button0 was pressed")
End Sub
Sub btn_pressed_Click
   Log ("button was pressed")
   Dim btn As Button
   btn = Sender
   Log ("button:" & btn.Tag)
   ToastMessageShow("pressed", False)
End Sub
 

pluton

Active Member
Licensed User
Longtime User
I quickly looka at code and I see you have

In Sub test_UI - Dim btn(50) As Button
In Sub p_Click - Dim btn As Button
In Sub btn_pressed_Click - Dim btn As Button

This is not a way to Dim something. Try to add their unique names
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
Change these lines:

B4X:
btn(i).Initialize("btn pressed0")

btn(i).Initialize("btn pressed")

To this:

B4X:
btn(i).Initialize("btn_pressed0")

btn(i).Initialize("btn_pressed")

And do you really want this?

B4X:
If (1 = 0) Then
 
Upvote 0

melamoud

Active Member
Licensed User
Longtime User
re you thinking the names of the variables are in conflict between local variables in separate sub?

This can't be the problem because:
1. Its local to the sub
2. It happened before I added the ok button
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
I quickly looka at code and I see you have

In Sub test_UI - Dim btn(50) As Button
In Sub p_Click - Dim btn As Button
In Sub btn_pressed_Click - Dim btn As Button

This is not a way to Dim something. Try to add their unique names

There isn't anything wrong with that. Variables declared in a subroutine can only be accessed from within the subroutine where they were declared and are destroyed when the subroutine ends.
 
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
2 problems.

"If (1 = 0) Then" should be "If (i = 0) Then"

And:
Your Click routines' names do not match what you initialized the buttons with.
Change this:

B4X:
 If (1 = 0) Then
                btn(i).Initialize("btn pressed0")
            Else
                btn(i).Initialize("btn pressed")
            End If
To this:
B4X:
 If (1 = 0) Then
                btn(i).Initialize("btnpressed0")
            Else
                btn(i).Initialize("btnpressed")
            End If

And your routines should be:
B4X:
Sub btnpressed0_Click
&
B4X:
Sub btnpressed_Click
 
Upvote 0
Top