Object array: How to?

dexMilano

Active Member
Licensed User
Longtime User
I've a group of panel (with an image inside) doing the same actions (to move by touch features).
How can I manage with a single piece of code (a single sub)?

Now I use the Sender and it seems working but I've 5 subs similar in which only the name of the sub (example pnl_w2_Touch) is different.

I think I can create an array of objects and manage then as 1.
But I don't know how to, an exemple could be the best.

TIA

dex
:sign0085:
 

dexMilano

Active Member
Licensed User
Longtime User
Hi Erel,

I tried but is doesn't seem to work

here you can see my code

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 fld_coords As EditText
   
   Dim fingerTop As Float
   Dim fingerLeft As Float
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   Dim pnl_w1,pnl_w2,pnl_w3,pnl_w4,pnl_w5,pnl_w6 As Panel
   Dim pnl_b1,pnl_b2,pnl_b3,pnl_b4,pnl_b5,pnl_b6 As Panel
   Dim pnl_gk, pnl_ball As Panel
   
   pnl_w1.Initialize ("panels")
   pnl_w2.Initialize ("panels")
   pnl_w3.Initialize ("panels")
   pnl_w4.Initialize ("panels")
....

Sub panels_Touch (Action As Int, X As Float, Y As Float)
   Dim s As Panel


   s = Sender
   Select Action
   Case 0      ' ACTION_DOWN
      fingerTop= Y
      fingerLeft= X
   Case 2      ' ACTION_MOVE
      s.Top = s.Top + Y - fingerTop
      s.Left= s.Left + X - fingerLeft
   Case 1      ' ACTION_UP
      Log("ACTION_UP")
   End Select
      
   DoEvents
   
   fld_coords.text= "Coords " & S.Tag & ": " & s.Left & "; " & s.Top
End Sub

I can't find the error.
I think I've used your suggestion in the right way.

:BangHead:
 
Upvote 0

dexMilano

Active Member
Licensed User
Longtime User
I used the touch to move the panels. following fingers

If I've dedicated sub (ex Sub pnl_w1_Touch(...)) everything works.
If I use the Initialize and the single sub nothing works at all.

Ihope it is clear.

dex
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Remove these lines if your panels were added to a layout in designer (your code suggests they were)
B4X:
   pnl_w1.Initialize ("panels")
   pnl_w2.Initialize ("panels")
   pnl_w3.Initialize ("panels")
   pnl_w4.Initialize ("panels")

Edit: in designer, you can make them all call the same subs by altering the "event name" to panels for them all
 
Last edited:
Upvote 0

Jamie

Member
Licensed User
Longtime User
Remove these lines if your panels were added to a layout in designer (your code suggests they were)
B4X:
   pnl_w1.Initialize ("panels")
   pnl_w2.Initialize ("panels")
   pnl_w3.Initialize ("panels")
   pnl_w4.Initialize ("panels")

Edit: in designer, you can make them all call the same subs by altering the "event name" to panels for them all

Really geat tips, Thanks you saved me from multiple button_Click subs calling 1 sub.
 
Upvote 0

dexMilano

Active Member
Licensed User
Longtime User
The Sub Touch commented was the original version that works perfectly.

THX for your help

:sign0085:

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 fld_coords As EditText
   
   Dim fingerTop As Float
   Dim fingerLeft As Float
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim pnl_w1,pnl_w2,pnl_w3,pnl_w4,pnl_w5,pnl_w6 As Panel
   Dim pnl_b1,pnl_b2,pnl_b3,pnl_b4,pnl_b5,pnl_b6 As Panel
   Dim pnl_gk, pnl_ball As Panel
   
   pnl_w1.Initialize ("panels")
   pnl_w2.Initialize ("panels")
   pnl_w3.Initialize ("panels")
   pnl_w4.Initialize ("panels")
   pnl_w5.Initialize ("panels")
   pnl_w6.Initialize ("panels")
   
   pnl_b1.Initialize ("panels")
   pnl_b2.Initialize ("panels")
   pnl_b3.Initialize ("panels")
   pnl_b4.Initialize ("panels")
   pnl_b5.Initialize ("panels")
   pnl_b6.Initialize ("panels")
   
   pnl_gk.Initialize ("panels")
   pnl_ball.Initialize ("panels")
   
   
   Activity.LoadLayout("frm_1")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub panels_Touch (Action As Int, X As Float, Y As Float)
   Dim s As Panel

   s = Sender
   Select Action
   Case 0      ' ACTION_DOWN
      fingerTop= Y
      fingerLeft= X
   Case 2      ' ACTION_MOVE
      s.Top = s.Top + Y - fingerTop
      s.Left= s.Left + X - fingerLeft
   Case 1      ' ACTION_UP
      Log("ACTION_UP")
   End Select
      
   DoEvents
   
   fld_coords.text= "Coords " & S.Tag & ": " & s.Left & "; " & s.Top
End Sub

'Sub pnl_w1_Touch (Action As Int, X As Float, Y As Float)
'Dim s As Panel
'
'   s = Sender
'   Select Action
'   Case 0      ' ACTION_DOWN
'      fingerTop= Y
'      fingerLeft= X
'   Case 2      ' ACTION_MOVE
'      s.Top = s.Top + Y - fingerTop
'      s.Left= s.Left + X - fingerLeft
'   Case 1      ' ACTION_UP
'      Log("ACTION_UP")
'   End Select
'      
'   DoEvents
'   
'   fld_coords.text= "Coords " & S.Tag & ": " & s.Left & "; " & s.Top
'End Sub
'
'
'Sub pnl_w2_Touch (Action As Int, X As Float, Y As Float)
'Dim s As Panel
'
'   s = Sender
'   Select Action
'   Case 0      ' ACTION_DOWN
'      fingerTop= Y
'      fingerLeft= X
'   Case 2      ' ACTION_MOVE
'      s.Top = s.Top + Y - fingerTop
'      s.Left= s.Left + X - fingerLeft
'   Case 1      ' ACTION_UP
'      Log("ACTION_UP")
'   End Select
'      
'   DoEvents
'   
'   fld_coords.text= "Coords " & S.Tag & ": " & s.Left & "; " & s.Top
'End Sub
'
'Sub pnl_w3_Touch (Action As Int, X As Float, Y As Float)
'Dim s As Panel
'
'   s = Sender
'   Select Action
'   Case 0      ' ACTION_DOWN
'      fingerTop= Y
'      fingerLeft= X
'   Case 2      ' ACTION_MOVE
'      s.Top = s.Top + Y - fingerTop
'      s.Left= s.Left + X - fingerLeft
'   Case 1      ' ACTION_UP
'      Log("ACTION_UP")
'   End Select
'      
'   DoEvents
'   
'   fld_coords.text= "Coords " & S.Tag & ": " & s.Left & "; " & s.Top
'End Sub
'
'Sub pnl_w4_Touch (Action As Int, X As Float, Y As Float)
'Dim s As Panel
'
'   s = Sender
'   Select Action
'   Case 0      ' ACTION_DOWN
'      fingerTop= Y
'      fingerLeft= X
'   Case 2      ' ACTION_MOVE
'      s.Top = s.Top + Y - fingerTop
'      s.Left= s.Left + X - fingerLeft
'   Case 1      ' ACTION_UP
'      Log("ACTION_UP")
'   End Select
'      
'   DoEvents
'   
'   fld_coords.text= "Coords " & S.Tag & ": " & s.Left & "; " & s.Top
'End Sub
'
'Sub pnl_w5_Touch (Action As Int, X As Float, Y As Float)
'Dim s As Panel
'
'   s = Sender
'   Select Action
'   Case 0      ' ACTION_DOWN
'      fingerTop= Y
'      fingerLeft= X
'   Case 2      ' ACTION_MOVE
'      s.Top = s.Top + Y - fingerTop
'      s.Left= s.Left + X - fingerLeft
'   Case 1      ' ACTION_UP
'      Log("ACTION_UP")
'   End Select
'      
'   DoEvents
'   
'   fld_coords.text= "Coords " & S.Tag & ": " & s.Left & "; " & s.Top
'End Sub
'
'Sub pnl_w6_Touch (Action As Int, X As Float, Y As Float)
'Dim s As Panel
'
'   s = Sender
'   Select Action
'   Case 0      ' ACTION_DOWN
'      fingerTop= Y
'      fingerLeft= X
'   Case 2      ' ACTION_MOVE
'      s.Top = s.Top + Y - fingerTop
'      s.Left= s.Left + X - fingerLeft
'   Case 1      ' ACTION_UP
'      Log("ACTION_UP")
'   End Select
'      
'   DoEvents
'   
'   fld_coords.text= "Coords " & S.Tag & ": " & s.Left & "; " & s.Top
'End Sub
'
'Sub pnl_b1_Touch (Action As Int, X As Float, Y As Float)
'Dim s As Panel
'
'   s = Sender
'   Select Action
'   Case 0      ' ACTION_DOWN
'      fingerTop= Y
'      fingerLeft= X
'   Case 2      ' ACTION_MOVE
'      s.Top = s.Top + Y - fingerTop
'      s.Left= s.Left + X - fingerLeft
'   Case 1      ' ACTION_UP
'      Log("ACTION_UP")
'   End Select
'      
'   DoEvents
'   
'   fld_coords.text= "Coords " & S.Tag & ": " & s.Left & "; " & s.Top
'End Sub
'
'Sub pnl_b2_Touch (Action As Int, X As Float, Y As Float)
'Dim s As Panel
'
'   s = Sender
'   Select Action
'   Case 0      ' ACTION_DOWN
'      fingerTop= Y
'      fingerLeft= X
'   Case 2      ' ACTION_MOVE
'      s.Top = s.Top + Y - fingerTop
'      s.Left= s.Left + X - fingerLeft
'   Case 1      ' ACTION_UP
'      Log("ACTION_UP")
'   End Select
'      
'   DoEvents
'   
'   fld_coords.text= "Coords " & S.Tag & ": " & s.Left & "; " & s.Top
'End Sub
'
'Sub pnl_b3_Touch (Action As Int, X As Float, Y As Float)
'Dim s As Panel
'
'   s = Sender
'   Select Action
'   Case 0      ' ACTION_DOWN
'      fingerTop= Y
'      fingerLeft= X
'   Case 2      ' ACTION_MOVE
'      s.Top = s.Top + Y - fingerTop
'      s.Left= s.Left + X - fingerLeft
'   Case 1      ' ACTION_UP
'      Log("ACTION_UP")
'   End Select
'      
'   DoEvents
'   
'   fld_coords.text= "Coords " & S.Tag & ": " & s.Left & "; " & s.Top
'End Sub
'
'Sub pnl_b4_Touch (Action As Int, X As Float, Y As Float)
'Dim s As Panel
'
'   s = Sender
'   Select Action
'   Case 0      ' ACTION_DOWN
'      fingerTop= Y
'      fingerLeft= X
'   Case 2      ' ACTION_MOVE
'      s.Top = s.Top + Y - fingerTop
'      s.Left= s.Left + X - fingerLeft
'   Case 1      ' ACTION_UP
'      Log("ACTION_UP")
'   End Select
'      
'   DoEvents
'   
'   fld_coords.text= "Coords " & S.Tag & ": " & s.Left & "; " & s.Top
'End Sub

.....
 
Upvote 0
Top