Android Question Problem with array button click new 2

juniorteacher

Member
Licensed 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
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
 

sorex

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

juniorteacher

Member
Licensed 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
 

sorex

Expert
Licensed 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.
 

sorex

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

juniorteacher

Member
Licensed 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)
 

sorex

Expert
Licensed 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
 

JordiCP

Well-Known Member
Licensed 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
 

juniorteacher

Member
Licensed 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
 
Top