A tout ceux qui ont besoin de créer un objet de plus pendant le code !
en 3 ligne de code vous pouvez créer tout type d'objets :
Dim ma As VB.TextBox
Objet_add ma, TextBox_, form1, 100, 400, 2000, 700, true
ma.text = "Text1"
je m'en suis surtout servie la dernière fois pour créer un Picturebox temporaire !
Dim picture_temp As VB.PictureBox
Objet_add picture_temp, PictureBox_, ma_form, 120, 600, , , False
....
....
Objet_remove picture_temp, ma_form
Source / Exemple :
Public Sub Objet_add(mon_objet As Object, ByVal mon_type_objet As Objet_add_, ma_form_desti As Form, ByVal X%, ByVal Y%, Optional ByVal lx%, Optional ByVal ly%, _
Optional visible As Boolean = True, Optional ByVal nom_de_mon_objet$ = "")
' créer dynamiquement des objets !
' exemple :
' Dim ma As VB.TextBox
' Objet_add ma, TextBox_, form1, 100, 400, 2000, 700, true
' ma.text = "Text1"
If nom_de_mon_objet$ = "" Then nom_de_mon_objet$ = Nom_to_aleatoire$
Select Case mon_type_objet
Case TextBox_
Set mon_objet = ma_form_desti.Controls.Add("VB.Textbox", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case ListBox_
Set mon_objet = ma_form_desti.Controls.Add("VB.Listbox", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case Combobox_
Set mon_objet = ma_form_desti.Controls.Add("VB.Combobox", nom_de_mon_objet$)
mon_objet.Width = lx%
Case CommandButton_
Set mon_objet = ma_form_desti.Controls.Add("VB.CommandButton", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case CheckBox_
Set mon_objet = ma_form_desti.Controls.Add("VB.CheckBox", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case DirListBox_
Set mon_objet = ma_form_desti.Controls.Add("VB.DirListBox", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case DriveListBox_
Set mon_objet = ma_form_desti.Controls.Add("VB.DriveListBox", nom_de_mon_objet$)
mon_objet.Width = lx%
Case FileListBox_
Set mon_objet = ma_form_desti.Controls.Add("VB.FileListBox", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case Frame_
Set mon_objet = ma_form_desti.Controls.Add("VB.Frame", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case HScrollBar_
Set mon_objet = ma_form_desti.Controls.Add("VB.HScrollBar", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case Image_
Set mon_objet = ma_form_desti.Controls.Add("VB.Image", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case Label_
Set mon_objet = ma_form_desti.Controls.Add("VB.Label", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case Line_
Set mon_objet = ma_form_desti.Controls.Add("VB.Line", nom_de_mon_objet$)
Case OptionButton_
Set mon_objet = ma_form_desti.Controls.Add("VB.OptionButton", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case PictureBox_
Set mon_objet = ma_form_desti.Controls.Add("VB.PictureBox", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case Shape_
Set mon_objet = ma_form_desti.Controls.Add("VB.Shape", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
Case VScrollBar_
Set mon_objet = ma_form_desti.Controls.Add("VB.VScrollBar ", nom_de_mon_objet$)
mon_objet.Width = lx%: mon_objet.Height = ly%
End Select
mon_objet.Left = X%: mon_objet.Top = Y%
If visible = True Then mon_objet.visible = True
End Sub
Public Sub Objet_remove(mon_objet As Object, ma_form_ou_ce_touve_lobjet As Form) ', Optional ByVal nom_de_mon_objet$ = "")
ma_form_ou_ce_touve_lobjet.Controls.Remove mon_objet
End Sub
Public Function Nom_to_aleatoire$(Optional ByVal Longueur_du_nom% = 8)
Randomize
For i% = 0 To Longueur_du_nom%
Nom_to_aleatoire$ = Nom_to_aleatoire$ + Chr$(65 + Rnd * 25)
Next i%
End Function
Conclusion :
petit cadeau la fonction Nom_to_aleatoire$ permet de créer aléatoirement un nom avec l'alphabet !
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.