cs_JMO
Messages postés1854Date d'inscriptionjeudi 23 mai 2002StatutMembreDernière intervention24 juin 2018
-
20 mars 2015 à 15:53
cs_JMO
Messages postés1854Date d'inscriptionjeudi 23 mai 2002StatutMembreDernière intervention24 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
cs_JMO
Messages postés1854Date d'inscriptionjeudi 23 mai 2002StatutMembreDernière intervention24 juin 201827 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é.
cs_JMO
Messages postés1854Date d'inscriptionjeudi 23 mai 2002StatutMembreDernière intervention24 juin 201827 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