Hi all,
I am getting some unexpected behavior in WinForms 2.0 with VS2005 SP1.
Scenario:
Start a new WindowsApplication project in vb.
Paste the following code into the .vb code behind of Form1, and the designer
code into the .Designer.vb.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.ItemDisabledToolStripMenuItem.HideDropDown()
End Sub
Private Sub ItemDisabled_ChildEnabledToolStripMenuItem_Click(ByVal
sender As System.Object, ByVal e As System.EventArgs) Handles
ItemDisabled_ChildEnabledToolStripMenuItem.Click
MessageBox.Show("OOPS!")
End Sub
End Class
<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)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
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()
Dim resources As System.ComponentModel.ComponentResourceManager =
New System.ComponentModel.ComponentResourceManager(GetType(Form1))
Me.ToolStrip1 = New System.Windows.Forms.ToolStrip
Me.ToolStripDropDownButton1 = New
System.Windows.Forms.ToolStripDropDownButton
Me.ItemEnabledToolStripMenuItem = New
System.Windows.Forms.ToolStripMenuItem
Me.ItemEnabled_ChildEnabledToolStripMenuItem = New
System.Windows.Forms.ToolStripMenuItem
Me.ItemDisabledToolStripMenuItem = New
System.Windows.Forms.ToolStripMenuItem
Me.ItemDisabled_ChildEnabledToolStripMenuItem = New
System.Windows.Forms.ToolStripMenuItem
Me.ToolStrip1.SuspendLayout()
Me.SuspendLayout()
'
'ToolStrip1
'
Me.ToolStrip1.Items.AddRange(New
System.Windows.Forms.ToolStripItem() {Me.ToolStripDropDownButton1})
Me.ToolStrip1.Location = New System.Drawing.Point(0, 0)
Me.ToolStrip1.Name = "ToolStrip1"
Me.ToolStrip1.Size = New System.Drawing.Size(626, 25)
Me.ToolStrip1.TabIndex = 0
Me.ToolStrip1.Text = "ToolStrip1"
'
'ToolStripDropDownButton1
'
Me.ToolStripDropDownButton1.DisplayStyle =
System.Windows.Forms.ToolStripItemDisplayStyle.Image
Me.ToolStripDropDownButton1.DropDownItems.AddRange(New
System.Windows.Forms.ToolStripItem() {Me.ItemEnabledToolStripMenuItem,
Me.ItemDisabledToolStripMenuItem})
Me.ToolStripDropDownButton1.Image =
CType(resources.GetObject("ToolStripDropDownButton1.Image"),
System.Drawing.Image)
Me.ToolStripDropDownButton1.ImageTransparentColor =
System.Drawing.Color.Magenta
Me.ToolStripDropDownButton1.Name = "ToolStripDropDownButton1"
Me.ToolStripDropDownButton1.Size = New System.Drawing.Size(29, 22)
Me.ToolStripDropDownButton1.Text = "ToolStripDropDownButton1"
'
'ItemEnabledToolStripMenuItem
'
Me.ItemEnabledToolStripMenuItem.DropDownItems.AddRange(New
System.Windows.Forms.ToolStripItem()
{Me.ItemEnabled_ChildEnabledToolStripMenuItem})
Me.ItemEnabledToolStripMenuItem.Name = "ItemEnabledToolStripMenuItem"
Me.ItemEnabledToolStripMenuItem.Size = New System.Drawing.Size(152,
22)
Me.ItemEnabledToolStripMenuItem.Text = "ItemEnabled"
'
'ItemEnabled_ChildEnabledToolStripMenuItem
'
Me.ItemEnabled_ChildEnabledToolStripMenuItem.Name =
"ItemEnabled_ChildEnabledToolStripMenuItem"
Me.ItemEnabled_ChildEnabledToolStripMenuItem.Size = New
System.Drawing.Size(212, 22)
Me.ItemEnabled_ChildEnabledToolStripMenuItem.Text =
"ItemEnabled_ChildEnabled"
'
'ItemDisabledToolStripMenuItem
'
Me.ItemDisabledToolStripMenuItem.DropDownItems.AddRange(New
System.Windows.Forms.ToolStripItem()
{Me.ItemDisabled_ChildEnabledToolStripMenuItem})
Me.ItemDisabledToolStripMenuItem.Enabled = False
Me.ItemDisabledToolStripMenuItem.Name =
"ItemDisabledToolStripMenuItem"
Me.ItemDisabledToolStripMenuItem.Size = New System.Drawing.Size(152,
22)
Me.ItemDisabledToolStripMenuItem.Text = "ItemDisabled"
'
'ItemDisabled_ChildEnabledToolStripMenuItem
'
Me.ItemDisabled_ChildEnabledToolStripMenuItem.Name =
"ItemDisabled_ChildEnabledToolStripMenuItem"
Me.ItemDisabled_ChildEnabledToolStripMenuItem.Size = New
System.Drawing.Size(214, 22)
Me.ItemDisabled_ChildEnabledToolStripMenuItem.Text =
"ItemDisabled_ChildEnabled"
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(626, 272)
Me.Controls.Add(Me.ToolStrip1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ToolStrip1.ResumeLayout(False)
Me.ToolStrip1.PerformLayout()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents ToolStrip1 As System.Windows.Forms.ToolStrip
Friend WithEvents ToolStripDropDownButton1 As
System.Windows.Forms.ToolStripDropDownButton
Friend WithEvents ItemEnabledToolStripMenuItem As
System.Windows.Forms.ToolStripMenuItem
Friend WithEvents ItemEnabled_ChildEnabledToolStripMenuItem As
System.Windows.Forms.ToolStripMenuItem
Friend WithEvents ItemDisabledToolStripMenuItem As
System.Windows.Forms.ToolStripMenuItem
Friend WithEvents ItemDisabled_ChildEnabledToolStripMenuItem As
System.Windows.Forms.ToolStripMenuItem
End Class
------------------------------------
This will give you a form with a tool strip containing one drop down button.
It's drop down items are one enabled and one disabled toolstripmenuitem.
The enabled item has an enabled "child", and the disabled item also has an
enabled "child". The disabled item also has had its HideDropDown method
called in form load.
Given this scenario, I would expect to not be able to reach the child item
of the disabled ToolStripMenuItem. In fact, if you drop down the toolstrip
dropdownbutton, and you FIRST go to the disabled ToolStripMenuItem, this
expected behavior is what you get. However, if you then shift focus to the
enabled ToolStripMenuItem, and then back to the disabled one, all of a sudden
its child is displayed!
Is this expected behavior or a bug?
Thanks.

Signature
John
JT - 28 Aug 2007 06:42 GMT
I apologize in advance to anyone trying this at home. The code got clipped
big time in pasting it into this post. It will take a bit of work to
recreate this project. Fortunately, its a simple one :)

Signature
John
> Hi all,
> I am getting some unexpected behavior in WinForms 2.0 with VS2005 SP1.
[quoted text clipped - 172 lines]
>
> Thanks.
Linda Liu [MSFT] - 28 Aug 2007 10:17 GMT
Hi John,
I performed a test based on your sample code and did reproduce the problem
on my side.
The default behavior of a disabled ToolStripMenuItem is that it won't open
its dropdown items when the user selects the ToolStripMenuItem. In fact, if
you comment out the line of code
'Me.ItemDisabledToolStripMenuItem.HideDropDown()' in the Form1 Load event
handler, the ItemDisabledToolStripMenuItem still won't open its dropdown
items at first, but open its dropdown items after you select the
ItemEnabledToolStripMenuItem.
I have looked this issue up in our internal database and found that this is
a known bug of ToolStripMenuItem. Maybe this bug will be fixed in the next
version of Visual Studio.
The workaround of the problem is to handle the ToolStripMenuItem.DropDown's
Opening event and if the owner item is disabled, cancel the Opening event.
The following is a sample code.
Public Class Form1
Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
AddHandler ItemDisabledToolStripMenuItem.DropDown.Opening,
AddressOf DropDownOpening
End Sub
Private Sub DropDownOpening(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs)
Dim tsdd As ToolStripDropDown = CType(sender, ToolStripDropDown)
If (Not (tsdd Is Nothing) And Not (tsdd.OwnerItem Is Nothing)) Then
e.Cancel = Not tsdd.OwnerItem.Enabled
End If
End Sub
End Class
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
JT - 29 Aug 2007 05:44 GMT
Thank you Linda. I was aware of the problem occuring even without the
HideDropDown code, as that was one of my attempts to fix the problem.
I had come up with the following:
Private Sub DisabledToolStripMenuItem_DropDownOpening(ByVal sender As
System.Object, ByVal e As System.EventArgs) _
Handles DisabledToolStripMenuItem.DropDownOpening
Dim ts As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)
If ts IsNot Nothing AndAlso ts.Enabled = False Then
For i As Integer = 0 To ts.DropDownItems.Count - 1
ts.DropDownItems(i).Enabled = False
Next
End If
End Sub
However, I like your method better as it prevents the drop down event from
occuring in the first place, obviating the need to disable the child items.
I appreciate your swift, expert, researched reply. The help you provide on
this message board is OUTSTANDING!!

Signature
John
> Hi John,
>
[quoted text clipped - 63 lines]
>
> This posting is provided "AS IS" with no warranties, and confers no rights.