ToolStripMenuItem : comment récupérer l'item sélectionné

Résolu
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 - 2 avril 2015 à 00:22
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 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'.

Quelle est mon erreur ?

Merci de vos suggestions,
jean-marc

2 réponses

cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
2 avril 2015 à 03:21
Salut

Pas compris
tu as
WithEvents ChoicePDP As ToolStripMenuItem
et

Dim ChoicePDP As ToolStripDropDownButton = New ToolStripDropDownButton
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
Modifié par cs_ShayW le 2/04/2015 à 03:42
peut etre ainsi

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
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
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é.

@+.
jean-marc
0
Rejoignez-nous