OBJET DYNAMIQUE

MadM@tt Messages postés 2167 Date d'inscription mardi 11 novembre 2003 Statut Membre Dernière intervention 16 juillet 2009 - 3 mai 2005 à 22:38
cs_franckydeluka Messages postés 228 Date d'inscription mardi 5 avril 2005 Statut Membre Dernière intervention 4 janvier 2008 - 24 févr. 2006 à 16:11
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/31167-objet-dynamique

cs_franckydeluka Messages postés 228 Date d'inscription mardi 5 avril 2005 Statut Membre Dernière intervention 4 janvier 2008 1
24 févr. 2006 à 16:11
Ton code est trop bien.
Merci c'est cool !!
cs_fouifouine Messages postés 2 Date d'inscription lundi 16 août 2004 Statut Membre Dernière intervention 14 mai 2005
14 mai 2005 à 01:04
c'est bien tous ça mais si vous pouvez penser aux débutants comme moi en ajoutant des commentaires

merci pour les débutants
bidoch78 Messages postés 67 Date d'inscription mercredi 27 février 2002 Statut Membre Dernière intervention 2 décembre 2005
13 mai 2005 à 17:29
J'ai pas trouvé à cause du probleme de déclaration de VB pour les variables avec evenements
Impossible de faire dim withevents Toto() as Label

Mais je peux te proposer la solution suivante (evidemment faite à l'arrache, vites fait, mais je pense que c'est un bon début, entre autre il faudrait faire quelque chose de totalement générique)

Création d'1 classe "clsObject"

Code :

Option Explicit

Private WithEvents oLabel As Label

Private oParent As clsObjects

'*******
'*******

Public Sub Dispose()
Set oParent = Nothing
End Sub

'*******
'*******

Public Property Get Label() As Label
Set Label = oLabel
End Property

Public Property Set Label(ByRef RHS As Label)
Set oLabel = RHS
End Property

Public Property Get Parent() As clsObjects
Set Parent = oParent
End Property

Public Property Set Parent(ByRef RHS As clsObjects)
Set oParent = RHS
End Property

Private Sub oLabel_Click()
Call oParent.RaiseEventClick(oLabel)
End Sub

Création d'1 classe "clsObjects"

Code :

Option Explicit

Public Event Click(ByRef oObject As Object)

Private m_ColObject As Collection

'***********************
'***********************

Private Sub Class_Initialize()
Set m_ColObject = New Collection
End Sub

Private Sub Class_Terminate()
Set m_ColObject = Nothing
End Sub

'***********************
'***********************

Public Function AddObject(ByVal pClassName As String, ByVal pName As String, ByRef pForm As Form, ByRef pContainer) As clsObject

Dim oNewObject As clsObject

Set oNewObject = New clsObject

Set oNewObject.Label = pForm.Controls.Add(pClassName, pName, pContainer)
Set oNewObject.Parent = Me

m_ColObject.Add oNewObject

Set AddObject = oNewObject
Set oNewObject = Nothing

End Function

Public Sub Remove()

While m_ColObject.Count > 0
m_ColObject(1).Dispose
m_ColObject.Remove 1
Wend

End Sub

Public Sub RaiseEventClick(ByRef oObject As Object)

RaiseEvent Click(oObject)

End Sub

Code Du Formulaire (Form1)

Option Explicit

Private WithEvents Labels As clsObjects

Private Sub Form_Load()

Set Labels = New clsObjects

With Labels.AddObject("VB.LABEL", "Lbl1", Me, Me).Label
.Caption = "Toto1"
.AutoSize = True
.Visible = True
End With

With Labels.AddObject("VB.LABEL", "Lbl2", Me, Me).Label
.Caption = "Toto2"
.AutoSize = True
.Visible = True
.Move 1000
End With

End Sub

Private Sub Form_Terminate()

Labels.Remove
Set Labels = Nothing

End Sub

Private Sub Labels_Click(oObject As Object)

If TypeOf oObject Is Label Then

Dim oLabel As Label

Set oLabel = oObject

MsgBox oLabel.Name

End If

End Sub
vbman34 Messages postés 2 Date d'inscription lundi 1 mars 2004 Statut Membre Dernière intervention 13 mai 2005
13 mai 2005 à 16:22
ok pour la gestion d'un textbox, mais pour un tableau de textBox :
textBox(0), textBox(1)... etc
tu dois gérer un evenement pour chaque objet? ou alors...
fkx Messages postés 44 Date d'inscription jeudi 29 janvier 2004 Statut Membre Dernière intervention 26 juin 2006
10 mai 2005 à 17:28
euh... je trouve pas les mots... Merci !!!
Depuis le temps que je cherchais comment faire !!!
Un 10 rien que pour ça !
MadM@tt Messages postés 2167 Date d'inscription mardi 11 novembre 2003 Statut Membre Dernière intervention 16 juillet 2009 1
4 mai 2005 à 16:20
c'est si simple que ça ? pourquoi se priver alors
bidoch78 Messages postés 67 Date d'inscription mercredi 27 février 2002 Statut Membre Dernière intervention 2 décembre 2005
4 mai 2005 à 13:38
Trankillou :

Private WithEvents oLabel As Label

Set oLabel = Form1.Controls.Add("VB.LABEL", "Lbl1", Form1)

oLabel.Caption = "toto"
oLabel.Visible = True

Private Sub oLabel_Click()
MsgBox "Ok"
End Sub

Et voili.
vbman34 Messages postés 2 Date d'inscription lundi 1 mars 2004 Statut Membre Dernière intervention 13 mai 2005
4 mai 2005 à 10:03
bon c pas pour te KC,
mais tu géres comment les événements du TextBox créé?
bidoch78 Messages postés 67 Date d'inscription mercredi 27 février 2002 Statut Membre Dernière intervention 2 décembre 2005
4 mai 2005 à 09:50
Pareil, j'avais jamais pensé à ca !!!
MadM@tt Messages postés 2167 Date d'inscription mardi 11 novembre 2003 Statut Membre Dernière intervention 16 juillet 2009 1
3 mai 2005 à 22:38
Tiens j'avais jamais vu ça auparavant, je découvre 8)
Rejoignez-nous