1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
Public Class Form1
Inherits System.Windows.Forms.Form
#Region "DESIGNER"
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Private components As System.ComponentModel.IContainer
Public Sub New()
Me.InitializeComponent()
End Sub
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.Panel1 = New System.Windows.Forms.Panel()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 10)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(48, 37)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Ajouter"
Me.Button1.UseVisualStyleBackColor = True
'
'Panel1
'
Me.Panel1.AutoScroll = True
Me.Panel1.Location = New System.Drawing.Point(12, 57)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(620, 350)
Me.Panel1.TabIndex = 2
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(644, 419)
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
'variable d'incrémentation pour le nommage unique des contrôles
Private gbIncrement As Integer = 0
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' creation Gbox
Dim gb As New GroupBox
' création dynamique du label1 dans le Gbox
Dim Lbl1 As New System.Windows.Forms.Label()
Lbl1.AutoSize = True
Lbl1.Location = New System.Drawing.Point(21, 31)
Lbl1.Name = "LB_" & gbIncrement.ToString
Lbl1.Size = New System.Drawing.Size(39, 13)
Lbl1.TabIndex = 0
Lbl1.Text = "LB_" & gbIncrement.ToString
Lbl1.Tag = "LB_" & gbIncrement.ToString
' création dynamique du TextBox1 dans le Gbox
Dim txb1 As New System.Windows.Forms.TextBox()
txb1.Location = New System.Drawing.Point(76, 28)
' On donne un nom unique au contrôle grace à la variable d'incrémentation "gbIncrement" (ATTENTION : nommage unique obligatoire sinon les gestionnaires d'évènements ne fonctionnent pas !!!)
txb1.Name = "TXB_" & gbIncrement.ToString
txb1.Size = New System.Drawing.Size(516, 20)
txb1.TabIndex = 1
txb1.Tag = "TXB_" & gbIncrement.ToString
' Association du gestionnaire d'évènements "TextBox1.TextChanged" à la Sub TextBox1_TextChanged
AddHandler txb1.TextChanged, AddressOf TextBox1_TextChanged
'Ajout des controles créés plus haut au container principal qui est le GroupBox
gb.Controls.Add(txb1)
gb.Controls.Add(Lbl1)
gb.Size = New System.Drawing.Size(614, 94)
gb.TabIndex = 1
gb.TabStop = False
gb.Name = "GB_" & gbIncrement.ToString
gb.Text = "GB_" & gbIncrement.ToString
gb.Location = New Point(3, (gb.Height + 3) * gbIncrement)
gb.Tag = "GB_" & gbIncrement.ToString
' Ajout du GroupBox au Panel(le panel1 est paramétré avec un scrollbar=True)
Me.Panel1.Controls.Add(gb)
' On incrémente la variable (Si suppression d'1 groupBox il faut penser à décrémenter cette variable !!!
gbIncrement += 1
End Sub
' C'est le gestionnaire d'évènement "TextChanged" de TextBox1
Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs)
Dim CurrentTxb1 As TextBox = TryCast(sender, TextBox)
MsgBox("La contenu a été modifié : " & CurrentTxb1.Text, MsgBoxStyle.OkOnly, CurrentTxb1.Name)
End Sub
End Class |
Partager