You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

StickyLabel是什么?为何出现在ContextMenuStrip集合及类型转换异常问题

问题分析与解决方案

为什么会出现StickyLabel转换错误?

首先,你的核心问题是遍历了错误的集合:ContextMenuStrip.Controls是用于存放上下文菜单容器内的辅助子控件,而非你手动添加的菜单项目(ToolStripMenuItem/ToolStripSeparator)。

那个StickyLabel是Microsoft Office/VSTO框架内部的一个辅助控件,通常用于在Office界面中显示临时提示或状态文本,会被VSTO自动注入到上下文菜单的控件集合中,不属于你定义的菜单项目,所以当你尝试把它强制转换为ToolStripItem时就会抛出类型转换异常。

你应该遍历的是ContextMenuStrip.Items集合——这个集合专门用于存储所有的菜单项目(也就是你添加的ToolStripMenuItem和ToolStripSeparator)。

修复后的代码

把循环对象从Controls改为Items,同时优化选中状态的判断逻辑,减少重复代码:

Private Sub ContextMenuStrip_PopulatedItems_Opening(sender As Object, e As CancelEventArgs) Handles ContextMenuStrip_PopulatedItems.Opening
    Dim ThisListView As ListView = TryCast(ContextMenuStrip_PopulatedItems.SourceControl, ListView)
    If ThisListView Is Nothing Then Return ' 额外判断,避免SourceControl不是ListView的情况

    Dim selectedCount As Integer = ThisListView.SelectedItems.Count
    Dim hasSelection As Boolean = selectedCount > 0
    Dim isSingleSelection As Boolean = selectedCount = 1
    Dim isMultipleSelection As Boolean = selectedCount > 1

    For Each MyControl As ToolStripItem In ContextMenuStrip_PopulatedItems.Items
        MyControl.Visible = False
        Dim tagValue As String = TryCast(MyControl.Tag, String)
        
        If String.IsNullOrEmpty(tagValue) Then
            ' 可根据需求选择隐藏或抛出异常,这里默认跳过
            Continue For
            ' Throw New ApplicationException("The control tag property is not set or invalid.")
        End If

        Select Case tagValue
            Case "ShowAlways"
                MyControl.Visible = True
            Case "WhenSelection"
                MyControl.Visible = hasSelection
            Case "WhenSingleSelection"
                MyControl.Visible = isSingleSelection
            Case "WhenMultipleSelection"
                MyControl.Visible = isMultipleSelection
            Case Else
                Throw New ApplicationException($"Unrecognized tag value: {tagValue}")
        End Select
    Next
End Sub

额外优化点

  1. 用TryCast替代直接强制转换SourceControl,避免上下文菜单被其他控件触发时出现异常
  2. 提前计算选中状态变量,减少重复访问SelectedItems.Count的次数,让代码更清晰
  3. 增加对Tag为空或非字符串类型的判断,避免出现额外的转换异常

内容的提问来源于stack exchange,提问作者Ama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:17:10