Android Question Problem with array button click new 2

juniorteacher

Member
Licensed User
Longtime User
i use array to make button,
how i identify that button i click?

B4X:
dim button(13) as button

for i =  0 to 13 -1
but(i).Initialize("But")
Activity.AddView(but(i),10%x + (i*11.5%x),14.5%y,11.5%x,8.3%y)
next

sub but(0)_click 'error in here
msgbox("test","test")
end sub

please give me an example
 

sorex

Expert
Licensed User
Longtime User
put the array number (i in your case) as tag and then you should be able to do

B4X:
sub but_click
dim b as button
b=sender
log(b.tag)
buttons(b.tag).enable=false
end sub

you better change the arrayname to buttons to avoid confusion
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
indeed Klaus, since it's the same button anyway that's even shorter.
 
Upvote 0

juniorteacher

Member
Licensed User
Longtime User
put the array number (i in your case) as tag and then you should be able to do

B4X:
sub but_click
dim b as button
b=sender
log(b.tag)
buttons(b.tag).enable=false
end sub

you better change the arrayname to buttons to avoid confusion

buttons ???
i'm already change it, but pull down menu did show the option for enable

Or simply:
B4X:
Sub But_Click
    Dim b as Button
    b = Sender
    b.Enable = false
End Sub
i have 13 button and give tag name every button
can it disable button from button tag name like
B4X:
Sub But_Click
    Dim b(13) as Button = sender
    Dim btnid as int = btn().tag ' error in here
b(12).Enable = false ' error in here
End Sub
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
it seems you don't really understand what you are writing there.

look again at my example and the one from Klaus. It's all there.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you will need to loop through the array and check the tags. if the right one passes you disable it.
 
Upvote 0

juniorteacher

Member
Licensed User
Longtime User
B4X:
dim buttons(13) as button
for i = 0 to 13 -1
buttons(i).Initialize("buttons")
buttons(i).tag = i
next

sub buttons_click
dim b as button = sender
bid = b.tag
b(btag).enable = false
end sub

i want buttons(0) can disable buttons(3) or buttons(5)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
B4X:
sub buttons_click
dim b as button = sender
bid = b.tag
b.enable = false
if bid=0 then
 buttons(3).enable=false
 buttons(5).enable=false
end if
end sub
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
I suppose the problem is that you are not declaring the buttons as Global variables. You must do so to be able to use them in the "buttons_click" event

B4X:
Sub Globals
  Dim buttons(13) as button
end sub
Sub Activity_Create(FirstTime as Boolean)
  for i = 0 to 13 -1
    buttons(i).Initialize("buttons")
    buttons(i).tag = i
  next
end sub

Sub buttons_click
  dim b as button = sender
  bid = b.tag
  if bid=0 then
    buttons(3).enable=false
    buttons(5).enable=false
  endif
end sub
 
Upvote 0

juniorteacher

Member
Licensed User
Longtime User
I suppose the problem is that you are not declaring the buttons as Global variables. You must do so to be able to use them in the "buttons_click" event

B4X:
Sub Globals
  Dim buttons(13) as button
end sub
Sub Activity_Create(FirstTime as Boolean)
  for i = 0 to 13 -1
    buttons(i).Initialize("buttons")
    buttons(i).tag = i
  next
end sub

Sub buttons_click
  dim b as button = sender
  bid = b.tag
  if bid=0 then
    buttons(3).enable=false
    buttons(5).enable=false
  endif
end sub

yes solved thanks very much.... that was true
 
Upvote 0
Top