Discussion:
[Gambas-user] Control arrays
Ahmad Kamal
2003-07-20 21:05:16 UTC
Permalink
hi all,

I am currently writing a TicTacToe game on GB. I already wrote it on VB,
so it's a matter of porting. The game of course consists of 9 identical
squares, in VB I made a control array. I know GB misses one, how can I do
something like:

for i =1 to 10
label(i).forecolor=vbGreen
next
Benoit Minisini
2003-07-20 22:08:42 UTC
Permalink
Post by Ahmad Kamal
hi all,
I am currently writing a TicTacToe game on GB. I already wrote it on VB,
so it's a matter of porting. The game of course consists of 9 identical
squares, in VB I made a control array. I know GB misses one, how can I do
for i =1 to 10
label(i).forecolor=vbGreen
next
No control arrays, but controls can be put in the same group. Do you see the
(Group) property in the IDE property sheet ?

Every control in the same group has the same event handlers. The prefix of
these event handlers is the name of the group.

This is explained in one of the tips of the days... that you can translate
into arabic too ;-)

Regards,
--
Benoit Minisini
mailto:***@users.sourceforge.net
Nelson Ferraz
2003-07-21 02:45:44 UTC
Permalink
Post by Benoit Minisini
Post by Ahmad Kamal
for i =1 to 10
label(i).forecolor=vbGreen
next
No control arrays, but controls can be put in the same group.
How do you iterate over controls?

It would be interesting to treat containers as collections, allowing
constructs like:

FOR EACH f IN Form
PRINT (f.name)
NEXT

It doesn't seem to work, though.

[]s

Nelson
Rob
2003-07-21 02:55:40 UTC
Permalink
Post by Nelson Ferraz
It would be interesting to treat containers as collections, allowing
FOR EACH f IN Form
PRINT (f.name)
NEXT
It doesn't seem to work, though.
However, Form inherits Window which inherits Container, and Container has a
"Children" property.... which I think you can iterate over.

I wonder if I could include an "inherited members" section on each page....
hmmm.

Rob
Fabien BODARD
2003-07-21 15:53:48 UTC
Permalink
Post by Rob
Post by Nelson Ferraz
It would be interesting to treat containers as collections, allowing
FOR EACH f IN Form
PRINT (f.name)
NEXT
It doesn't seem to work, though.
However, Form inherits Window which inherits Container, and Container has a
"Children" property.... which I think you can iterate over.
I wonder if I could include an "inherited members" section on each page....
hmmm.
Rob
-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
Gambas-user mailing list
https://lists.sourceforge.net/lists/listinfo/gambas-user
Hi,

There is another way ...
With this :

Public Image1 as Collection[]
Public Const Zone_Size as Integer = 60
Public Sub New()
Dim i as integer
Dim j as integer
Dim obj as Image
For i = 0 to 2
For j = 0 to 2
Obj = New Image(Me) as "Image1"
Obj.Move (i * Zone_Size, j * Zone_Size, Zone_Size, Zone_Size)
Image1.Add(Obj)
Next
Next

end

So you can Manage the Image properties by :
Image1[3] .Picture = Picture["img/cross.png"]

And the events by :

Public Sub Image1_Click()
End

With gambas we can emulate the vb controle usual use.

Gambas is really today the beginning of a new powered basic implementation, in
the little world of the good programming linguages.


Fabien BODARD
Ahmad Kamal
2003-07-20 22:16:01 UTC
Permalink
Post by Benoit Minisini
No control arrays, but controls can be put in the same group. Do you see the
(Group) property in the IDE property sheet ?
Every control in the same group has the same event handlers. The prefix of
these event handlers is the name of the group.
This is explained in one of the tips of the days... that you can translate
into arabic too ;-)
Yes, but groups only handle multiple event handling, but changing the
forecolor of a lot of labels is not doable by groups, or is it? DO I really
have to
label1.text="1"
label2.text="2"
label3.text="3"
...
Rob
2003-07-20 22:18:46 UTC
Permalink
Post by Ahmad Kamal
squares, in VB I made a control array. I know GB misses one, how can I do
for i =1 to 10
label(i).forecolor=vbGreen
next
Gambas doesn't have any way to make an array of controls graphically yet. You
can assign them to groups for handling of events, but that doesn't really
give you any of the other benefits of control arrays. The best I've come up
with is to do it once in code and then treat it the same as in VB. Warning:
pseudo-code ahead, I don't have any projects handy where this is done.

public Labels as Object[]

...

(in form load event handler)
labels = new Object[10]
labels[0] = Label0
labels[1] = Label1
...
labels[9] = Label9

...

(then, in some other routine)
for i =1 to 10
labels(i).forecolor=Color.Green
next

If you have more than a handful of controls to be put into the array, it might
be better to create the controls in code at runtime rather than using the
form editor and manually putting them into the array.

In any case, you would then want to put all those controls into a group (in
code it's something like mycontrol = new Label as Labels) so that you can
handle all the events with one bit of code.

Rob
Ahmad Kamal
2003-07-20 22:41:19 UTC
Permalink
Post by Rob
In any case, you would then want to put all those controls into a group (in
code it's something like mycontrol = new Label as Labels) so that you can
handle all the events with one bit of code.
Rob
Thnx A LOT, Rob, but could u plz show how to put them in a group with code,
in my specific case(labels)? I am new to not making an array graphically.
Rob
2003-07-20 23:59:56 UTC
Permalink
Post by Ahmad Kamal
Thnx A LOT, Rob, but could u plz show how to put them in a group with code,
in my specific case(labels)? I am new to not making an array graphically.
Setting the group of a control must be done at control creation time. That
means, if you create the control graphically you need to do it graphically
(in the Properties dialog) and if you want to set the group in code you also
have to create the control in code.

In your case you would want to do something like this, supposing 100x100
labels (please refer to one of the Tips of the Day which has the exact syntax
for grouping, if mine doesn't compile):

Public Labels as Object[]

and then in the form load event handler

dim tmpLabel as Label
labels = new Object[]

tmpLabel = new Label(ME) as "Labels"
tmpLabel.Move(0,0,100,100)
tmpLabel.Text = "text of label 0"
labels.Add(tmpLabel) ' this will add label 0

tmpLabel = new Label(ME) as "Labels"
tmpLabel.Move(100,0,100,100)
tmpLabel.Text = "text of label 1"
labels.Add(tmpLabel) ' this will add label 1

...

tmpLabel = new Label(ME) as "Labels"
tmpLabel.Move(200,200,100,100)
tmpLabel.Text = "text of label 8"
labels.Add(tmpLabel) ' this will add label 8

Then when it's time to set the event handlers,

Public Sub Labels_Click()

... your handler code...

END

Obviously it's a lot more code intensive than just being able to set the index
in the form designer, but it can be done. It actually works much better if
you have like a hundred controls to create because then it's more efficient
to do it in a loop.

I guess what would be ideal as a workaround for the lack of design time
control arrays would be either (a) a collection or object array automatically
populated with the contents of the corresponding group name, or (b) the
ability to add controls to a given array and set the index independently of
the group feature. I think either could be implemented in the IDE (via code
automatically generated and added to the form's class file) without needing
changes to the language, if this proves unwieldy at the language level. (a)
was pretty much how I mean to do it in my .frm to .form/.class converter
which isn't really doing much of use yet, but (b) would probably work better
for non-converted forms (since if you're converting from a VB project you
already know which index each control is supposed to have.)

Rob
Benoit Minisini
2003-07-21 18:53:13 UTC
Permalink
Post by Ahmad Kamal
hi all,
I am currently writing a TicTacToe game on GB. I already wrote it on VB,
so it's a matter of porting. The game of course consists of 9 identical
squares, in VB I made a control array. I know GB misses one, how can I do
for i =1 to 10
label(i).forecolor=vbGreen
next
I'm not sure that arrays of controls defined at development time is useful.

If all the controls have the same container, then the following is sufficient
to browse the controls :

FOR EACH hCtrl IN hContainer.Children
...
NEXT

If all the controls must have the same behaviour, the (Group) property is
sufficient.

In your case, if you want to use nine times the same control to make the TIC
TAC TOE game, then I suggest you creating them at runtime. This way, you will
be able to create a N x N game easily.

' Note: the following is a compound array, a VB-like array. This is completely
' different from Object[] that is a object array, ie a Java-like array.
' The compound array is allocated inside the object where it is declared.
' The other array is a real object allocated on the heap.

PRIVATE hSquare[3, 3] AS Object

FOR X = 0 TO 2
FOR Y = 0 TO 2
hSquare[X, Y] = NEW Label(hContainer) AS "TicTacToeSquare"
...
NEXT
NEXT

Regards,
--
Benoit Minisini
mailto:***@users.sourceforge.net
Continue reading on narkive:
Loading...