Button Arrays?

Draccus

New member
Feb 7, 2011
0
0
0
Visit site
I've made a custom checkbox with two images. One for an empty box and a second one with a checkmark in the box. It works fine but I'm trying to figure out how to do this with 7 buttons. In C# I would probably make some sort of array of buttons so that they can do something like (pseudo C# code)

Button Buttons = new Button[7];
foreach ( Button b in Buttons )
b.Image = "cbempty.png";

void Button1_Click ()
{
ToggleButton(0);
}

void Button2_Click ()
{
ToggleButton(1);
}

void ToggleButton ( int index )
{
Buttons[index].Image = (Buttons[index].Image=="cbempty.png")?"cbchecked.png":"cbempty.png";
return;
}

The above is something like I want to accomplish. I would rather not make a seperat if statement for every single button.
 

rshives

New member
Nov 16, 2010
2
1
0
Visit site
One way you could solve the problem is to create the buttons you need. Then place them all inside a List.

VARIABLE -> MAKE A LIST -> Button1
-> Button2
-> Button3

Then you could iterate through the list or use list indexes (which start at 1 btw) as in your psudo code.
 

Draccus

New member
Feb 7, 2011
0
0
0
Visit site
Okay so far I figured this much out.

Buttons -> Make a list -> (component) Button 1
(all buttons, 7 in all)

The next problem is hit when trying to assign a value to the Image field.

foreach variable (name) temp
var b = temp
var b.Image = "checked.png";
in list (global) Buttons

The problem is that b is just a variable. It has two blocks and no properties from the Button. Though it seems that the array list is full of buttons, the access right now seems like a normal variable (either set or get values, not specific fields).
 

rshives

New member
Nov 16, 2010
2
1
0
Visit site
That is a limitation with App Inventor. You cannot dynamically set the UI element (or any element for that matter) properties.

Even though you have a List of Buttons, the List doesn't know it holds Button types.

I'm not sure you can solve this problem using App Inventor yet.