Quel évenement à utiliser pour GroupBox créé dynamiquement

Résolu
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 20 mars 2015 à 15:53
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 20 mars 2015 à 21:20
Bonjour le Forum,

Dans Form1, je créé, en dynamique, plusieurs GroupeBox dans lesquels j'insère des boutons (nombre aléatoire car issu pour l'instant, en test, d'un array).

Option Strict On
Public Class Form1

Private WithEvents GroupBox1 As New GroupBox
Private WithEvents GroupBox2 As New GroupBox

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Add the buttons to the GroupBox
GroupBox1_Create()
GroupBox2_Create()
End Sub

Private Sub GroupBox1_Create()
' Création GroupeBox1 et ajout Buttons.
Dim intPos As Integer = 20
Dim i As Integer = 1

Dim array(2) As String
array(0) = "Client 1"
array(1) = "Client 2"
array(2) = "Client 3"

For Each element As String In array
Dim Button As New Button()

Button.Location = New Point(15, intPos)
intPos = intPos + 42
Button.Height = 40
Button.Width = 120
Button.Text = element
Button.Name = "Button" & i
i = i + 1
Me.GroupBox1.Controls.Add(Button)
Next

' Ajout Groupbox1 dans Form1.
GroupBox1.Top = 10
GroupBox1.Left = 10
GroupBox1.Width = intPos + 20
GroupBox1.Height = intPos + 20
GroupBox1.Text = "Génération Plan de Production"
Me.Controls.Add(GroupBox1)
'AddHandler GroupBox1 , AddressOf ......
End Sub

Private Sub GroupBox2_Create()
' Création GroupeBox2 et ajout Buttons.
Dim intPos As Integer = 20
Dim i As Integer = 1

Dim array(4) As String
array(0) = "Consignes Client 1"
array(1) = "Consignes Client 2"
array(2) = "Consignes Client 3"
array(3) = "Consignes Client 4"
array(4) = "Consignes Client 5"

For Each element As String In array
Dim Button As New Button()

Button.Location = New Point(15, intPos)
intPos = intPos + 42
Button.Height = 40
Button.Width = 120
Button.Text = element
Button.Name = "Button" & i
i = i + 1
Me.GroupBox2.Controls.Add(Button)
Next

' Ajout Groupbox2 dans Form1.
GroupBox2.Top = 10
GroupBox2.Left = 200
GroupBox2.Width = 150
GroupBox2.Height = intPos + 20
GroupBox2.Text = "Création Consignes Client"
Me.Controls.Add(GroupBox2)
'AddHandler GroupBox1 , AddressOf ......
End Sub

' Private Sub groupBox1_???? (ByVal sender As Object, ByVal e As EventArgs) Handles GroupBox1

'Dim btn As Button = DirectCast(sender, Button)
' MessageBox.Show(btn.Text & " vient d'etre cliqué")
'End Sub
End Class


Quel évènement puis-je utilisé au clic d'un des boutons ???

Merci pour suggestions,
jean-marc

3 réponses

cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 137
20 mars 2015 à 17:14
0
ucfoutu Messages postés 18038 Date d'inscription lundi 7 décembre 2009 Statut Modérateur Dernière intervention 11 avril 2018 211
20 mars 2015 à 18:15
Bonjour,
J'ai peut-être mal compris le problème.
Mais si j'ai bien compris, alors ===>>
http://www.java2s.com/Tutorial/VB/0220__Event/AddHandlerbtnClickAddressOfbtnClick.htm
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
20 mars 2015 à 21:20
Merci Le Pivert et Jacques,

Le lien du AddHandler m'a permis de résoudre mon problème.
        For Each element As String In MyArray
Dim MyButton As New Button()

MyButton.Location = New Point(15, intPos)
MyButton.Height = 40
MyButton.Width = 150
MyButton.Text = element
MyButton.Name = MyLabel.Text
intPos = intPos + 42
i = i + 1
Me.Controls.Add(MyButton)
AddHandler MyButton.Click, AddressOf MyButton_Click
Next

    Private Sub MyButton_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyButton.Click
Dim btn As Button = DirectCast(sender, Button)
MessageBox.Show(btn.Text & vbTab & btn.Name, "Contrôle")
End Sub


En mettant le label.text dans le btn.name cela me permet de savoir dans quel groupbox virtuel se trouve le bouton cliqué.

Bonne soirée,
@+
jean-marc
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
20 mars 2015 à 18:33
Re-bonjour,

En testant avec l'évènement "FlowLayoutPanel1", j'ai l'erreur :
FlowLayoutPanel1 n est pas un membre de WindowsApplication1.Form1


Option Strict On
Public Class Form1
'Private WithEvents FlowLayoutPanel1 As System.Windows.Forms.FlowLayoutPanel

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Add the buttons to the MyPanel1, MyPanel2, MyPanel3 et MyPanel4
MyPanel1_Create()
End Sub

Private Sub MyPanel1_Create()
' Création Panel1 et ajout Buttons.
Dim MyPanel1 As New Panel
Dim intPos As Integer = 20
Dim i As Integer = 1

Dim MyArray(2) As String
MyArray(0) = "Client 1"
MyArray(1) = "Client 2"
MyArray(2) = "Client 3"

For Each element As String In MyArray
Dim MyButton As New Button()
AddHandler MyButton.Click, AddressOf HandleDynamicButtonClick
MyButton.Location = New Point(15, intPos)
MyButton.Height = 40
MyButton.Width = 120
MyButton.Text = element
MyButton.Name = "Button" & i
MyButton.Tag = MyPanel1
intPos = intPos + 42
i = i + 1
MyPanel1.Controls.Add(MyButton)
Me.FlowLayoutPanel1.Controls.Add(MyPanel1) 'ligne en erreur
'FlowLayoutPanel1 n est pas un membre de WindowsApplication1.Form1
Next
End Sub

Private Sub HandleDynamicButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
Dim panel1 As Panel = DirectCast(btn.Tag, Panel)

MessageBox.Show(btn.Text & vbTab & panel1.Name)
End Sub

End Class


re-bonjour Jacques,
Merci pour lien, je regarde !!!
jean-marc
0
Rejoignez-nous