Afficher un message
Vieux 30/08/2005, 11h45   #4 (permalink)
_Thierry
XLDnaute Barbatruc
 
Date d'inscription: février 2005
Messages: 3 100
Par défaut => DEMO AutoGeneration UserForm par Programmation

Bonjour Ludo, le Forum

Arf oui les Pauvres petits Labels....

Pour ton sas, sans le code je ne sais pas trop dans quoi tu 'farfouilles' mais si, admettons, j'avais à générer un UserForm avec 40 TextBoxs accompagnées de 40 Labels le tout alligné en collonne de 10 en 10

Ceci avec ces 40 Labels qui devraient donc réagir au Click avec un MsgBox... Tout en conservant le UserForm ainsi généré avec tous ses Controls ActiveX, voici comme je procèderai :

Citation:
OptionExplicit

'================================R E A D - M E======================================
'
'        NB Nécessite la Réference VB à Microsoft Form 2.0 Object Library
'          Paramétrage Sécurité 'Faire Confiance au Projet Visual Basic'
'================================================= ==================================

Const Sign AsString = '@+Thierry 's Truc sur www.Excel-Downloads.com, Aug 2005'
Const USFName AsString = 'USF_Auto_Thierry'

Const TxbWidth AsInteger = 65
Const TxbHeigth AsInteger = 15
Const TxbLeft AsInteger = 90
Const TxbTop AsInteger = 15

Const LblWidth AsInteger = 70
Const LblHeigth AsInteger = 15
Const LblLeft AsInteger = 10
Const LblTop AsInteger = 15

Sub MyUserFormAutoBuilder()
Dim ObjUSF AsObject
Dim ObjTextBox As Object, ObjLabel As Object, CmdB As Object, LstB AsObject
Dim TopPlusHeight AsInteger
Dim x AsByte
Dim VLblLeft AsInteger
Dim VTxbLeft AsInteger

 
 
Set ObjUSF = ThisWorkbook.VBProject.VBComponents.Add(3)
 
With ObjUSF
    .Properties('Caption') = Sign
    .Properties('Width') = 660
    .Properties('Height') = 195
    .Properties('ShowModal') =
True
'    .Properties('Name') = USFName 'Si on veut forcer un Nom de UserForm...
 
EndWith
 
   
For x = 1 To 40
       
     
Set ObjTextBox = ObjUSF.Designer.Controls.Add('Forms.TextBox.1')
     
Set ObjLabel = ObjUSF.Designer.Controls.Add('Forms.Label.1')
     
         
SelectCase x
           
Case 1 To 10
             
If x = 1 Then TopPlusHeight = LblTop
              VLblLeft = LblLeft
              VTxbLeft = TxbLeft
           
Case 11 To 20
             
If x = 11 Then TopPlusHeight = LblTop
              VLblLeft = LblLeft + 160
              VTxbLeft = TxbLeft + 160
           
Case 21 To 30
             
If x = 21 Then TopPlusHeight = LblTop
              VLblLeft = LblLeft + 320
              VTxbLeft = TxbLeft + 320
           
Case 31 To 40
             
If x = 31 Then TopPlusHeight = LblTop
              VLblLeft = LblLeft + 480
              VTxbLeft = TxbLeft + 480
         
EndSelect
             
         
         
With ObjLabel
            .Caption = 'Label TextBox ' & x
            .Left = VLblLeft: .Top = TopPlusHeight: .Width = LblWidth: .Height = LblHeigth
            .Tag = 'Thierry
's Demo'
            .Name = 'LblDemo' & x
         
EndWith
         
With ObjTextBox
            .Left = VTxbLeft: .Top = TopPlusHeight: .Width = TxbWidth: .Height = TxbHeigth
            .Tag = 'Thierry
's Demo'
            .Name = 'TxbDemo' & x
            .TextAlign = fmTextAlignRight
         
EndWith
         
        TopPlusHeight = TopPlusHeight + 15
   
Next

MyLabelClicks ObjUSF.Name

VBA.UserForms.Add(ObjUSF.Name).Show
 
Set ObjUSF = Nothing
Set ObjTextBox = Nothing
Set ObjLabel = Nothing

EndSub

Sub MyLabelClicks(MyUsf As String)
Dim x AsInteger
Dim L AsByte

   
With ThisWorkbook.VBProject.VBComponents(MyUsf).CodeMod ule
       
For L = 1 To 40
            x = .CountOfLines
            .InsertLines x + 1, 'Sub LblDemo' & L & '_Click()'
            .InsertLines x + 2, 'MsgBox ''Je suis la Label Numero ' & L & ''
            .InsertLines x + 3, 'End Sub'
       
Next
   
EndWith

EndSub

Bonne Journée
[ol]@+Thierry[/ol]
_Thierry est déconnecté   Réponse avec citation