Visual Basic >
Visual Basic Forums
>
Visual Basic General
>
Disable click event on ToolStripSeparator
Disable click event on ToolStripSeparator
- I have a MenuStrip created at design time with a number of ToolStripItems and ToolStripSeparators. By default when a user clicks on one of the separators the menu closes but nothing happens (as there is nothing in the separator click event).
I would like the menu to stay open if a user clicks on the separator, but how do I handle this?
Answers
- Here you go:
Public Class myToolStripMenuItem Inherits ToolStripMenuItem Public Sub New() MyBase.New() Me.DropDown = New myToolStripDropDownMenu End Sub End Class Public Class myToolStripDropDownMenu Inherits ToolStripDropDownMenu Protected Overrides Sub OnItemClicked(ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) If Not TypeOf e.ClickedItem Is ToolStripSeparator Then MyBase.OnItemClicked(e) End If End Sub End Class
- Proposed As Answer byKFrostILEM Wednesday, January 21, 2009 9:44 AM
- Marked As Answer byXingwei HuMSFT, ModeratorThursday, January 22, 2009 3:05 AM
All Replies
- Have you tried RemoveHandler?
Asgar - Yes, I tried that and it does prevent the Click event being fired (if there were any code in the ToolStripSeparator.Click event) but it still closes the menu and the user has to re-open it and make their selection.
In Visual Studio, for example, if you click on the separator, nothing happens and the menu stays open. This is the behaviour I want in my app. - I tried the following and nothing worked:
Public Class mySeparator Inherits System.Windows.Forms.ToolStripSeparator Protected Overrides Sub OnClick(ByVal e As System.EventArgs) 'MyBase.OnClick(e) End Sub Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs) 'MyBase.OnMouseDown(e) End Sub Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs) 'MyBase.OnMouseUp(e) End Sub Protected Overrides ReadOnly Property DismissWhenClicked() As Boolean Get Return False 'Return MyBase.DismissWhenClicked End Get End Property Protected Overrides ReadOnly Property CanRaiseEvents() As Boolean Get Return False 'Return MyBase.CanRaiseEvents End Get End Property Public Overrides ReadOnly Property CanSelect() As Boolean Get Return False 'Return MyBase.CanSelect End Get End Property Overrides End Class
I think this must be an issue with what the parent ToolStripMenu is doing when it registers that one of its child items has been clicked, because as far as I can see, this class ignores mouseclicks completely now, so it cannot be the separator itself that is causing the issue.
I think that perhaps overriding the DismissWhenClicked property of the item that is spawning the menu might help.
Thanks for your idea. The end result of this custom class is that clicking on the item closes the menu which is the same as the regular ToolStripSeparator.
The parent menu item is a ToolStripDropDownMenu which doesn't have a DismissWhenClicked property so I am stuck there. I read a post about handling the Closing event of a DropDownMenu but I can't seem to find the options for that either.- Here you go:
Public Class myToolStripMenuItem Inherits ToolStripMenuItem Public Sub New() MyBase.New() Me.DropDown = New myToolStripDropDownMenu End Sub End Class Public Class myToolStripDropDownMenu Inherits ToolStripDropDownMenu Protected Overrides Sub OnItemClicked(ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) If Not TypeOf e.ClickedItem Is ToolStripSeparator Then MyBase.OnItemClicked(e) End If End Sub End Class
- Proposed As Answer byKFrostILEM Wednesday, January 21, 2009 9:44 AM
- Marked As Answer byXingwei HuMSFT, ModeratorThursday, January 22, 2009 3:05 AM
- Excellent. Works perfectly if I change the code:
Me.DropDown=New myToolStripDropDownMenu
to create a DropDownMenu and populate it with items via code because it then opens the correct DropDownMenu. However, if I add it at design-time and add regular MenuItems and Separators when I click the control at run-time nothing happens because it is creating a new myToolStripDropDownMenu object.
If I recreate my menu structure in code, how can I associate the code with the click event of a coded menuitem? I have only ever used VS to create menu items and write code for the events in the drop down menus provided by the IDE.
Your help is much appreciated... - Yikes.
I've reopened my test project, and all sorts of weird stuff is happening with my dropdown menus. Sometimes clicking on the dropdown opens the dropdown menu in the designer, sometimes it doesn't. Sometimes the entire menuStrip disappears. Occaisionally, it actually opens a dropdown menu, and that actually does what it's supposed to...
Damned if I know. It worked when I wrote the code, and it works if I start a new project and start again, but after that it may or may not work, basically at random...
I've tried a couple of other things, including repopulating the dropDownMenu every time it is called, but the results remain very inconsistent.
How very weird.
- I know this is a little late, but I was having the same problem and I wanted to share the answer I came up with.
Instead of setting the DropDown property in the constructor of myToolStripMenuItem, you want to override the CreateDefaultDropDown method to set the drop down. So the above code will look like:
This will give you designer support.Public Class myToolStripMenuItem Inherits ToolStripMenuItem Protected Overrides Function CreateDefaultDropDown() As ToolStripDropDown Dim menu As New myToolStripDropDownMenu menu.OwnerItem = Me Return menu End Function End Class Public Class myToolStripDropDownMenu Inherits ToolStripDropDownMenu Protected Overrides Sub OnItemClicked(ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) If Not TypeOf e.ClickedItem Is ToolStripSeparator Then MyBase.OnItemClicked(e) End If End Sub End Class
- Proposed As Answer byshawn.ohern Thursday, October 01, 2009 5:00 PM
- In fact there is a far more easier way to achieve this:
Set the Enabled property of the ToolStripSeparator to false. This will prevent the control from receiving any input, just like disabling any ToolStripMenuItem.
And by the way many thanks to the developing team for their enlightening documentation of Enabled property on ToolStripSeparator class:
"Infrastructure. This property is not relevant to this class." Whatever.


