[Gambas-user] How to use events with an object in object[]?

Laurent Carlier lordheavym at ...626...
Sat Dec 29 17:01:44 CET 2007


Le Saturday 29 December 2007 16:40:36 Leonardo Miliani, vous avez écrit :
> I'm writing a little program (a game using a chessboard and some pawns)
> and I would like to use a collection of PictureBoxes inside an Object[].
> This is my code:
>
> PRIVATE BlackPawns AS NEW Object[]
> PRIVATE WithePawns AS NEW Object[]
> PRIVATE BlackPawn AS PictureBox
> PRIVATE WhitePawn AS PictureBox
>
> PUBLIC SUB _new()
> DIM varI AS Integer
> DIM varX AS Integer
>
>   varX = 48
>   FOR varI = 0 TO 2
>     BlackPawn = NEW PictureBox(ME)
>     BlackPawn = picture.Load(Application.Path & "/graphics/blackpawn.png")
>     BlackPawn.BackColor = IIf((varI MOD 2) = 0, &h000000, &hffffff)
>     BlackPawn.Name = "BlackPawn" & Trim(CStr(varI))
>     BlackPawn.X = varX
>     BlackPawn.Y = 72
>     WhitePawn = NEW PictureBox(ME)
>     WhitePawn.Picture = picture.Load(Application.Path &
> "/graphics/whitepawn.png")
>     WhitePawn.BackColor = IIf((varI MOD 2) = 0, &h000000, &hffffff)
>     WhitePawn.Name = "WhitePawn" & Trim(CStr(varI))
>     WhitePawn.X = varX
>     WhitePawn.Y = 172
>
>     BlackPawns.Add(BlackPawn)
>     WhitePawns.Add(WhitePawn)
>     varX = varX + 50
>   NEXT
> END
>
> The pictureboxes are created, put inside the array Object[] and showed
> on the chessboard. Now I would like to manage the MouseDown event to
> change the picture of the selected pawn to show a different one that
> should mean I selected it.
>
> But I cannot manage them using the normal event methods...
> I mean that if I write a piece of code like this:
>
> PUBLIC SUB WhitePawns_MouseDown()
>
>   LAST.picture = picture.Load(Application.Path &
> "/graphics/whitepawn_sel.png")
>
> END
>
> It doesn't do anything...

You need to define the event handler name with the AS keyword
http://64.128.110.55/help/lang/as

eg:
BlackPawn = NEW PictureBox(ME) AS "PawnBlack"
so you got 
PUBLIC SUB PawnBlack_MouseDown()

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20071229/61cc2fd4/attachment.sig>


More information about the User mailing list