'What is a StickyLabel, and why does it sit in a ContextMenuStrip.Contols collection?
I have a ContextMenuStrip
which is tight to a ListView
, and made of a combination of various ToolStripMenuItem
and ToolStripSeparator
.
I would like this context menu to hide/show some if these controls, depending on the current status of the ListView
(no item in the view, only 1 item selected, more than 1 item selected).
The ListView
is part of a UserControl which is loaded in Ms Word via an application-level VSTO AddIn.
At the moment, my strategy was to rely on the .tag
property of the controls and to run a switch over this property for each control in the context menu.
It looks like this:
Private Sub ContextMenuStrip_PopulatedItems_Opening(sender As Object, e As CancelEventArgs) Handles ContextMenuStrip_PopulatedItems.Opening
Dim ThisListView As ListView = ContextMenuStrip_PopulatedItems.SourceControl
For Each MyControl As ToolStripItem In ContextMenuStrip_PopulatedItems.Controls
MyControl.Visible = False
Select Case CType(MyControl.Tag, String)
Case "ShowAlways"
MyControl.Visible = True
Case "WhenSelection"
If ThisListView.FocusedItem IsNot Nothing AndAlso ThisListView.SelectedItems.Count > 0 Then
MyControl.Visible = True
End If
Case "WhenSingleSelection"
If ThisListView.FocusedItem IsNot Nothing AndAlso ThisListView.SelectedItems.Count = 1 Then
MyControl.Visible = True
End If
Case "WhenMultipleSelection"
If ThisListView.FocusedItem IsNot Nothing AndAlso ThisListView.SelectedItems.Count > 1 Then
MyControl.Visible = True
End If
Case Else
Throw New ApplicationException("The control tag property is not recognized.")
End Select
Next
End Sub
When I right click on the ListView
, I get the following error message:
System.InvalidCastException: 'Unable to cast object of type 'StickyLabel' to type 'System.Windows.Forms.ToolStripItem'.'
at the line: For Each MyControl As ToolStripItem In ContextMenuStrip_FilesToPopulate.Control
.
I did not find any documentation nor responses on google when searching for "StickyLabel". And I do not understand why it is trying to cast it in my ForeEach loop, instead of just skipping it?
Solution 1:[1]
For Each MyControl As ToolStripItem In ContextMenuStrip_PopulatedItems.Controls
should be changed to
For Each MyControl As ToolStripItem In ContextMenuStrip_PopulatedItems.Items
.
However, I am still interested to understand what is happening behind the scenes with this StickyLabel.
Solution 2:[2]
Those Labels are used for ToolStripDropDown List, that exceed the display size. You get a button to the top end bottom of the list, which scroll through the list in the appropriate direction.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ama |
Solution 2 | Christian Pflugradt |