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.
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.
yes, but how call i the items, that i've created with MenuStrip1.Items.Add?
I want from the clicked Item the ToolTipText.
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.
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.ItemClickedFor Each ChildForm As Form1 In Me.MdiChildrenIf ChildForm.WindowID = ITEM.ToolTipText ThenChildForm.WindowState = FormWindowState.Maximized
End IfNextEnd Sub
The big and italic written "ITEM" is now my problem. How can i solve that?
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.ClickEnd Sub
The sender of the MenuStrip item clicked event is always the menustrip no matter the item clicked
@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.
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 SubUSE
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....
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.
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.
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.
" 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*
Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClickedEnd Sub
Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClickedMsgBox(Sender.Text)
End Sub
PLEASE TAKE NOTE OF TESTED CODE:
< Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _Partial Class Form1Inherits 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 Thencomponents.Dispose() End IfMyBase.Dispose(disposing) End Sub'Required by the Windows Form DesignerPrivate 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.MenuStripMe.ToolStripMenuItem1 = New System.Windows.Forms.ToolStripMenuItemMe.ToolStripMenuItem2 = New System.Windows.Forms.ToolStripMenuItemMe.ToolStripMenuItem3 = New System.Windows.Forms.ToolStripMenuItemMe.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.FontMe.ClientSize = New System.Drawing.Size(563, 266) Me.Controls.Add(Me.MenuStrip1) Me.MainMenuStrip = Me.MenuStrip1Me.Name = "Form1"Me.Text = "Form1"Me.MenuStrip1.ResumeLayout(False) Me.MenuStrip1.PerformLayout() Me.ResumeLayout(False) Me.PerformLayout() End SubFriend WithEvents MenuStrip1 As System.Windows.Forms.MenuStripFriend WithEvents ToolStripMenuItem1 As System.Windows.Forms.ToolStripMenuItemFriend WithEvents ToolStripMenuItem2 As System.Windows.Forms.ToolStripMenuItemFriend WithEvents ToolStripMenuItem3 As System.Windows.Forms.ToolStripMenuItem End Class |
Public End
Dim m As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)
'Debug.Print(m.Name) Object Reference exceptionDim ms As MenuStrip = TryCast(sender, MenuStrip)
Debug.Print(ms.Name)
End Sub
***************************
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'MenuStrip1by 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.ClickDim tsmi As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)Debug.Print(tsmi.Name)End Sub
Which produces the following output:
'ToolStripMenuItem1'ToolStripMenuItem2'ToolStripMenuItem3In 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