Gambas France BETA


Pas de compte ? Incription

Comment créer evenement unique sur plusieurs objets crées dynamiquement

1
AuteurMessages
caracole#1 Posté le 20/12/2010 à 13:21:00
Bonjour,

Je me lance dans Gambas ... Ca change de VB !

Dans l'exemble ci-dessous je cree des boutons... ca fonctionne bien
cependant, je suis obligé de créer une procedure _click pour chaque bouton !
Je n'ai pas trouvé le moyen de créer une procedure click uniqaue et recuperer un evenement.
au lieu de créer Btn_evt_n_Click()
Quel est le moyen ?

Merci.
Exemple
1
' Gambas class file' Test creation dynamique de boutonsPUBLIC SUB _new()DIM i AS IntegerDIM btns[10] AS ButtonDIM hButton AS ButtonDIM widthbutton AS Integer = 48DIM heightbutton AS Integer = 24DIM Xbutton AS Integer = 10DIM Ybutton AS Integer = 10i = 0FOR i = 0 TO 8 hButton = NEW Button(ME) AS "Btn_evt_" & i WITH hButton .Caption = "BTN " & i .Width = widthbutton .Height = heightbutton .X = Xbutton + (i * widthbutton) .Y = Ybutton END WITH btns[i] = hButtonNEXTENDPUBLIC SUB Form_Open()ENDPUBLIC SUB Btn_evt_0_Click() ME.caption = "Click 0" ENDPUBLIC SUB Btn_evt_1_Click() ME.caption = "Click 1" ENDPUBLIC SUB Btn_evt_2_Click() ME.caption = "Click 2" END
Prokopy#2 Posté le 20/12/2010 à 16:48:00
Kinder PinguiSalut,

Si tu veux que tous tes boutons réagissent exactement pareil, tu peux utiliser le même nom d'évènement pour tous les boutons. ;)
Exemple :

1
DIM widthbutton AS INTEGER = 48DIM heightbutton AS INTEGER = 24DIM Xbutton AS INTEGER = 10DIM Ybutton AS INTEGER = 10i = 0FOR i = 0 TO 8 hButton = NEW Button(ME) AS "Event_bouton" WITH hButton .Caption = "BTN " & i .Width = widthbutton .Height = heightbutton .X = Xbutton + (i * widthbutton) .Y = Ybutton END WITH btns[i] = hButtonNEXTENDPUBLIC SUB Form_Open()ENDPUBLIC SUB Event_bouton_Click() PRINT "clic !" END
La théorie, c'est quand on sait tout et que rien ne fonctionne.
La pratique, c'est quand ça marche mais qu'on ne sait pas pourquoi.
Quand la théorie rejoint la pratique, rien ne fonctionne et on ne sait pas pourquoi.
caracole#3 Posté le 20/12/2010 à 17:16:00
Salut,

Si tu veux que tous tes boutons réagissent exactement pareil, tu peux utiliser le même nom d'évènement pour tous les boutons. ;)
Exemple :

1
DIM widthbutton AS INTEGER = 48DIM heightbutton AS INTEGER = 24DIM Xbutton AS INTEGER = 10DIM Ybutton AS INTEGER = 10i = 0FOR i = 0 TO 8 hButton = NEW Button(ME) AS "Event_bouton" WITH hButton .Caption = "BTN " & i .Width = widthbutton .Height = heightbutton .X = Xbutton + (i * widthbutton) .Y = Ybutton END WITH btns[i] = hButtonNEXTENDPUBLIC SUB Form_Open()ENDPUBLIC SUB Event_bouton_Click() PRINT "clic !" END


D'accord, mais ca ne me dit pas quel bouton a été cliqué !
Prokopy#4 Posté le 20/12/2010 à 18:17:00
Kinder PinguiPour ça, tu peux utiliser le mot-clé Last dans ton évènement qui renvoie le bouton qui a été cliqué. Par exemple :

1
DIM widthbutton AS INTEGER = 48DIM heightbutton AS INTEGER = 24DIM Xbutton AS INTEGER = 10DIM Ybutton AS INTEGER = 10i = 0FOR i = 0 TO 8 hButton = NEW Button(ME) AS "Event_bouton" WITH hButton .Caption = "BTN " & i .Width = widthbutton .Height = heightbutton .X = Xbutton + (i * widthbutton) .Y = Ybutton END WITH btns[i] = hButtonNEXTENDPUBLIC SUB Event_bouton_Click() PRINT LAST.Caption END


Ce code t'affichera le contenu du bouton cliqué. :)
Cela marche bien entendu avec toutes les propriétés (comme Tag par exemple).
La théorie, c'est quand on sait tout et que rien ne fonctionne.
La pratique, c'est quand ça marche mais qu'on ne sait pas pourquoi.
Quand la théorie rejoint la pratique, rien ne fonctionne et on ne sait pas pourquoi.
caracole#5 Posté le 20/12/2010 à 19:53:00
Pour ça, tu peux utiliser le mot-clé Last dans ton évènement qui renvoie le bouton qui a été cliqué. Par exemple :

1
DIM widthbutton AS INTEGER = 48DIM heightbutton AS INTEGER = 24DIM Xbutton AS INTEGER = 10DIM Ybutton AS INTEGER = 10i = 0FOR i = 0 TO 8 hButton = NEW Button(ME) AS "Event_bouton" WITH hButton .Caption = "BTN " & i .Width = widthbutton .Height = heightbutton .X = Xbutton + (i * widthbutton) .Y = Ybutton END WITH btns[i] = hButtonNEXTENDPUBLIC SUB Event_bouton_Click() PRINT LAST.Caption END


Ce code t'affichera le contenu du bouton cliqué. :)
Cela marche bien entendu avec toutes les propriétés (comme Tag par exemple).


Merci, Je vais essayer..
Mais j'ai l'impression que c'est moins pratique que la propriété Index de VB .... enfin à voir..
Pierre
jeanyvon#6 Posté le 21/12/2010 à 07:44:00
Gambas? Ma! Et gusto

Merci, Je vais essayer..
Mais j'ai l'impression que c'est moins pratique que la propriété Index de VB .... enfin à voir..
Pierre

Bonjour,
Non, non je t'assure il faut simplement changer ses habitudes
Joyeux Noël
JY
Vieillir? On peut retarder mais pas y échapper!
caracole#7 Posté le 21/12/2010 à 09:19:00

Merci, Je vais essayer..
Mais j'ai l'impression que c'est moins pratique que la propriété Index de VB .... enfin à voir..
Pierre
r,
Non, non je t'assure il faut simplement changer ses habitudes
Joyeux Noël
JY[/Quote]

Merci JY,
avec Gambas, je découvre autre autre-chose... C'est plus interessant pour moi à mon âge que faire du sukodu !
chaque fois que j'utilise cet outil, je découvre de nouvelles possibilités..

Joyeuses fêtes idem
Ici on va se gaver de Gambas cette navidad !

Pierre
1