How to access the clicked item?

Hello,

with wich variable can i access the clicked item in a MenuStrip?

I′ve tested it with MenuStrip1.ClickedItem but that doesn′t works.

Please help me. Thanks.

[187 byte] By [softwarejaeger] at [2007-12-23]
# 1
I'm not sure I understand what you're asking. Each menu item has it's own Click event; just double-click the menu item in the designer and it will add the click handler procedure into your code for you.
Richard_Wolf at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

yes, but how call i the items, that i've created with MenuStrip1.Items.Add?

I want from the clicked Item the ToolTipText.

softwarejaeger at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

If you're adding the items in code, then you'll also have to add the handling procedure manually, something like:

Addhandler Menustrip1.Items(1).Click, AddressOf MyClickProcedure

The first object that is passed in to the click event ('sender') will be the menu item that was clicked.

Richard_Wolf at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

No, that couldn′t be the right for my problem.

I create a new Form and a new Item in the MenuStrip, (tabbed mdi) so, the form has an public string ID="Window1", "Window2", "Window3". This string is setted, when i click on the "New-Window-Button". So, the Item in the MenuStrip gets the ID from the Window as ToolTipText.

Now i want to maximize the Form, when i click on the Item in the MenuStrip, so i have this script:

Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

For Each ChildForm As Form1 In Me.MdiChildren

If ChildForm.WindowID = ITEM.ToolTipText Then

ChildForm.WindowState = FormWindowState.Maximized

End If

Next

End Sub

The big and italic written "ITEM" is now my problem. How can i solve that?

softwarejaeger at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

If you are looking for which "item" was clicked you need to use the individual item clicked event

Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click

End Sub

The sender of the MenuStrip item clicked event is always the menustrip no matter the item clicked

DMan1 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 6
Change 'ITEM' to 'sender'.
Richard_Wolf at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7

@DMan1

Yes, i want this, i want to know which item i clicked on.

Your code doesn't works, i don't know why.

@Richard_Wolf

I've tested that, but it doesn't works.

softwarejaeger at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8

each item of as menustrip has its own click event...why not use the individual click event:

instead of:

Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

End Sub

USE

Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click

End Sub

or the relevant menu item that was clicked...the above code does not work because the sender of the menustrip item clicked event is always the menustrip not the menu item....

DMan1 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 9

i could use your code, but i've written that i make MDI, so i have open and delete windows and so i (don't) need so much Items in my MenuBar.

What could i do, in VB6 the code works, it looks like this:

Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
Dim btn As Button
Dim frm As Form

' Find this window.
For Each frm In Forms
If frm.Caption = Button.ToolTipText Then frm.WindowState = 2
Next frm

If Not (frm Is Nothing) Then
frm.WindowState = vbNormal
frm.Show
End If

End Sub

There i haven't an String "ID", there it checked, if the ToolTipText is the same as the Caption, but in matter of principle it's the same.

softwarejaeger at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 10

The 'sender' the 'SENDER'! That will tell you which menu item is clicked. Have you tried that? (Is there an echo?)

Private Sub SubFredToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubFredToolStripMenuItem.Click

Dim m As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)

Debug.WriteLine(m.ToolTipText)

End Sub

Even so, why don't you store a reference to the frm in the Menu Items Tag? This way you have direct access to the form you want without having to loop/compare.

SJWhiteley at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 11

I think the problem may be he was using the MenuStrip click event instead of the MenuStripItem click event. In the ItemClickEvent, the sender is indeed the menu item, not the parent MenuStrip. I failed to notice in your first example you were checking the MenuStrip not the Item click, sorry.

Stephen makes a very good point above, though. The 'Tag' property is no longer a type of String, it is Object. Therefore, you could set the Tag property to the form itself when you create it and just use Item.Tag.Windowstate = Maximized instead of a search loop.

Richard_Wolf at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 12

" Even so, why don't you store a reference to the frm in the Menu Items Tag? This way you have direct access to the form you want without having to loop/compare."

what do you mean? I'm just a beginner in VB.NET.

This codes doesn't works that you've written, i get everytime an Exception or Handle error. *CONFUSED*

softwarejaeger at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 13
Ok. From the top...

In the IDE, when you double click on the menu item in question, it should take you to the code editor, and show you something like the following:

 Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

End Sub


The first line is the subroutine constructor. It defines the name of the sub (MenuStrip1_ItemClicked), the passed parameters (ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs), and the event that the sub is meant to handle (MenuStrip1.ItemClicked).

What you are looking for is the actual menu item that you clicked to trigger this sub. That can be found in the passed parameter "sender". That means that a copy of the actual menu item has been passed to this sub, and you have direct access to it within that sub, right up until the "End Sub" line. All you have to do is refer to "sender", like below:

 Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

MsgBox(Sender.Text)

End Sub


If you copy that into your source, it should pop up a little message box that echoes the caption of the item you clicked. Any other member property or method should also be available to you from there as well, not just .Text

To repeat: The "sender" variable that is passed into the event handler sub IS the clicked menu item.
MarkWarner at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 14

PLEASE TAKE NOTE OF TESTED CODE:


<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

Partial Class Form1

Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.

<System.Diagnostics.DebuggerNonUserCode()> _

Protected Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso components IsNot Nothing Then

components.Dispose()
End If

MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

<System.Diagnostics.DebuggerStepThrough()> _

Private Sub InitializeComponent()
Me.MenuStrip1 = New System.Windows.Forms.MenuStrip

Me.ToolStripMenuItem1 = New System.Windows.Forms.ToolStripMenuItem

Me.ToolStripMenuItem2 = New System.Windows.Forms.ToolStripMenuItem

Me.ToolStripMenuItem3 = New System.Windows.Forms.ToolStripMenuItem

Me.MenuStrip1.SuspendLayout()
Me.SuspendLayout()
'

'MenuStrip1

'

Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ToolStripMenuItem1, Me.ToolStripMenuItem2, Me.ToolStripMenuItem3})
Me.MenuStrip1.Location = New System.Drawing.Point(0, 0)
Me.MenuStrip1.Name = "MenuStrip1"

Me.MenuStrip1.Size = New System.Drawing.Size(563, 24)
Me.MenuStrip1.TabIndex = 0
Me.MenuStrip1.Text = "MenuStrip1"

'

'ToolStripMenuItem1

'

Me.ToolStripMenuItem1.Name = "ToolStripMenuItem1"

Me.ToolStripMenuItem1.Size = New System.Drawing.Size(115, 20)
Me.ToolStripMenuItem1.Text = "ToolStripMenuItem1"

'

'ToolStripMenuItem2

'

Me.ToolStripMenuItem2.Name = "ToolStripMenuItem2"

Me.ToolStripMenuItem2.Size = New System.Drawing.Size(115, 20)
Me.ToolStripMenuItem2.Text = "ToolStripMenuItem2"

'

'ToolStripMenuItem3

'

Me.ToolStripMenuItem3.Name = "ToolStripMenuItem3"

Me.ToolStripMenuItem3.Size = New System.Drawing.Size(115, 20)
Me.ToolStripMenuItem3.Text = "ToolStripMenuItem3"

'

'Form1

'

Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font

Me.ClientSize = New System.Drawing.Size(563, 266)
Me.Controls.Add(Me.MenuStrip1)
Me.MainMenuStrip = Me.MenuStrip1

Me.Name = "Form1"

Me.Text = "Form1"

Me.MenuStrip1.ResumeLayout(False)
Me.MenuStrip1.PerformLayout()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub

Friend WithEvents MenuStrip1 As System.Windows.Forms.MenuStrip

Friend WithEvents ToolStripMenuItem1 As System.Windows.Forms.ToolStripMenuItem

Friend WithEvents ToolStripMenuItem2 As System.Windows.Forms.ToolStripMenuItem

Friend WithEvents ToolStripMenuItem3 As System.Windows.Forms.ToolStripMenuItem

End Class


Public Class Form1

Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked

Debug.Print(sender.ToString)
Dim m As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)
'Debug.Print(m.Name) Object Reference exception

Dim ms As MenuStrip = TryCast(sender, MenuStrip)
Debug.Print(ms.Name)
End Sub

End Class

***************************

In the above code I have taken a blank form and placed a single menustrip control at the top of the form. I then place 3 seperate Toolstripmenuitems on the menustrip. No other controls on the form...I run the application and click once on all three menu item the results of those actions are output as follows:

'System.Windows.Forms.MenuStrip, Name: MenuStrip1, Items: 3

'MenuStrip1

'System.Windows.Forms.MenuStrip, Name: MenuStrip1, Items: 3

'MenuStrip1

'System.Windows.Forms.MenuStrip, Name: MenuStrip1, Items: 3

'MenuStrip1

by trying to cast the sender object to a toolstripmenuitem causes a null reference exception because a menustrip item (THE SENDER) can not be cast to a Toolstripmenuitem....

I know how the sender typically works and the remarks about sender was what I originally posted in this thread....however after actually testing my code and the other code stationg that the sender should be cast....IT DOES NOT WORK.

A solution in which the sender would work is if we use an individual routine to handle all of the toolstripmenuitems click event such as this...

Private Sub ToolStripMenuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click, ToolStripMenuItem2.Click, ToolStripMenuItem3.Click

Dim tsmi As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)

Debug.Print(tsmi.Name)

End Sub

Which produces the following output:

'ToolStripMenuItem1

'ToolStripMenuItem2

'ToolStripMenuItem3

In order to keep this thread clean, It would be nice for all of you to test your code as I did and make the necesary corrections to your post

DMan1

DMan1 at 2007-8-30 > top of Msdn Tech,Visual Basic,Visual Basic Language...