cs_JMO
Messages postés1854Date d'inscriptionjeudi 23 mai 2002StatutMembreDernière intervention24 juin 2018
-
2 avril 2015 à 00:22
cs_JMO
Messages postés1854Date d'inscriptionjeudi 23 mai 2002StatutMembreDernière intervention24 juin 2018
-
2 avril 2015 à 08:56
Bonsoir le forum,
Nouveau en .Net (VB 2010 Express), j'essaie de créer dynamiquement un ToolStripMenuItem et de récupérer l'item sélectionné.
Public Class Form3 Inherits System.Windows.Forms.Form WithEvents ChoicePDP As ToolStripMenuItem
Public Function Main() Application.Run(New Form3) End Function
Public Sub New() Dim ListPDP = New ToolStrip Dim ChoicePDP As ToolStripDropDownButton = New ToolStripDropDownButton ChoicePDP.Text = "Génération Plan de Production" ChoicePDP.ForeColor = Color.Red ListPDP.Items.Add(ChoicePDP)
For i As Integer = 0 To 5 ChoicePDP.DropDownItems.Add(New ToolStripMenuItem(i)) Next Controls.Add(ListPDP) AddHandler ChoicePDP.Click, AddressOf WhichMenuItem_Click End Sub
Private Sub WhichMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ChoicePDP.DropDownItemClicked Dim MenuItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem) MessageBox.Show(MenuItem.Name) End Sub End Class
Au clic sur Choice.PDP, j'obtiens l'erreur sur le DirectCast :
L'exception System.InvalidCastException n'a pas été gérée
Message=Impossible d'effectuer un cast d'un objet de type 'System.Windows.Forms.ToolStripDropDownButton' en type 'System.Windows.Forms.ToolStripMenuItem'.
Public Class form3 Inherits System.Windows.Forms.Form WithEvents ChoicePDP As New ToolStripMenuItem
Private ListPDP As New ToolStrip Public Function Main() Application.Run(New Form3) End Function Public Sub New()
'Dim ChoicePDP As ToolStripDropDownButton = New ToolStripDropDownButton ChoicePDP.Text = "Génération Plan de Production" ChoicePDP.ForeColor = Color.Red ListPDP.Items.Add(ChoicePDP) Dim menu As ToolStripMenuItem For i As Integer = 0 To 5 menu = New ToolStripMenuItem menu.Name = "menuname" & i.ToString menu.Text = "menu" & i.ToString ChoicePDP.DropDownItems.Add(menu) AddHandler menu.Click, AddressOf WhichMenuItem_Click Next Controls.Add(ListPDP) End Sub
Private Sub WhichMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ChoicePDP.DropDownItemClicked
MessageBox.Show(DirectCast(sender, ToolStripMenuItem).Name) End Sub End Class
cs_JMO
Messages postés1854Date d'inscriptionjeudi 23 mai 2002StatutMembreDernière intervention24 juin 201826 2 avril 2015 à 08:56
Bonjour le Forum,
Bonjour ShayW,
Merci d'avoir corrigé mes erreurs de débutant.
Le messageBox me retourne le bon item cliqué.