Vb.net 如何在自定义ToolStripItem中显示菜单文本?

Vb.net 如何在自定义ToolStripItem中显示菜单文本?,vb.net,Vb.net,Mainform VB.Net: <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> Partial Class Form1 Inherits System.Windows.Forms.Form 'Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonU

Mainform VB.Net:

<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()
        Me.tsBasket = New System.Windows.Forms.ToolStrip()
        Me.tsiFruit = New System.Windows.Forms.ToolStripDropDownButton()
        Me.tsBasket.SuspendLayout()
        Me.SuspendLayout()
        '
        'tsBasket
        '
        Me.tsBasket.Dock = System.Windows.Forms.DockStyle.None
        Me.tsBasket.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden
        Me.tsBasket.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.tsiFruit})
        Me.tsBasket.Location = New System.Drawing.Point(355, 213)
        Me.tsBasket.Name = "tsBasket"
        Me.tsBasket.Size = New System.Drawing.Size(121, 25)
        Me.tsBasket.TabIndex = 5
        '
        'tsiFruit
        '
        Me.tsiFruit.ImageTransparentColor = System.Drawing.Color.Magenta
        Me.tsiFruit.Name = "tsiFruit"
        Me.tsiFruit.Size = New System.Drawing.Size(87, 22)
        Me.tsiFruit.Text = "add fruit"
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(800, 450)
        Me.Controls.Add(Me.tsBasket)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.tsBasket.ResumeLayout(False)
        Me.tsBasket.PerformLayout()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Friend WithEvents tsBasket As ToolStrip
    Friend WithEvents tsiFruit As ToolStripDropDownButton
End Class
自定义类:

Public Class TestTool
    Inherits ToolStripItem

    Private newPropertyValue As String
    Public Property NewProperty() As String
        Get
            Return newPropertyValue
        End Get
        Set(ByVal value As String)
            newPropertyValue = value
        End Set
    End Property

    Sub New()

        ' This call is required by the designer.
        ' InitializeComponent()

    End Sub
End Class
在我的自定义类/用户控件上,我得到:
设计器必须创建类型为“System.Windows.Forms.ToolStripItem”的实例,但不能创建,因为该类型已声明为抽象。
而且我没有看到菜单项中出现文本,但单击事件工作正常

这是扩展(使用OO继承)ToolMenuStrip的正确方法吗?这样我就可以有一个显示值和一个成员值,我希望能够在不使用
.Tag
的情况下存储一个对象

编辑: 尝试使用嵌套菜单时,我发现:

System.InvalidCastException:“无法将类型为”System.Windows.Forms.ToolStripMenuItem“的对象强制转换为类型为“WindowsApp1.TestTool”。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim men As New TestTool()
    men.Text = "showmeplease"
    men.Image = Nothing
    men.NewProperty = "BO"

    Dim men2 As New TestTool()
    men2.Text = "showmeplease2"
    men2.Image = Nothing
    men2.NewProperty = "BO2"

    tsiFruit.DropDownItems.Add(men)
    tsiFruit.DropDownItems.Add(men2)

    Dim TypeMenuItem As TestTool = men.DropDownItems.Add("hh")

    For Each mInfo As String In Moreinfo
        TypeMenuItem.DropDownItems.Add(mInfo, Nothing, AddressOf tsiType_Click)
    Next
    AddHandler men.Click, AddressOf tsiType_Click
    AddHandler men2.Click, AddressOf tsiType_Click
End Sub

Private p_Moreinfo As List(Of String)
Public Property Moreinfo() As List(Of String)
    Get

        Dim test As New List(Of String)
        test.Add("A")
        test.Add("B")
        Return test
    End Get
    Set(ByVal value As List(Of String))
        p_Moreinfo = value
    End Set
End Property
如果启用检查(您应该),您将在
Form1\u Load
事件中发现以下行的错误:

'错误:选项Strict On不允许从ToolStrinpItem进行隐式转换
'测试工具。
Dim TypeMenuItem As TestTool=men.DropDownItems.Add(“hh”)
因为
.DropDownItems.Add(String)
重载返回的是
ToolStripItem
,而不是
TestTool
类型的对象。
.DropDownItems
不会继承所有者项目的类型

编译。 Dim TypeMenuItem As ToolStripItem=men.DropDownItems.Add(“hh”) “编译。 Dim TypeMenuItem作为ToolStripMenuItem=DirectCast(men.DropDownItems.Add(“hh”),ToolStripMenuItem) '引发System.InvalidCastException。 Dim TypeMenuItem As TestTool=DirectCast(men.DropDownItems.Add(“hh”),TestTool) 因此,
Load
事件中的代码应该如下所示:

Private Sub Form1\u Load(发送方作为对象,e作为事件参数)处理MyBase.Load
Dim men作为新的测试工具{
.Text=“showmeeplease”,
.NewProperty=“BO”
}
Dim men2作为新的测试工具,具有{
.Text=“showmeplease2”,
.NewProperty=“BO2”
}
tsiFruit.DropDownItems.Add(男性)
tsiFruit.DropDownItems.Add(men2)
使用{.NewProperty=“hh”,.Text=“SomeText”}将TypeMenuItem作为新测试工具()进行调整
men.DropDownItems.Add(TypeMenuItem)
对于Moreinfo中的每个mInfo作为字符串
Dim item=newtesttool,带有{.NewProperty=mInfo、.Text=“SomeText”}
AddHandler项。单击,tsiType的地址\u单击
TypeMenuItem.DropDownItems.Add(项目)
下一个
AddHandler人。单击,tsiType的地址\u单击
AddHandler men2.单击,tsiType的地址\u单击
端接头
同样,为避免在
tsiType\u事件中引发异常,请单击
事件,以防万一:

Private子tsiType\u单击(发送者作为对象,e作为事件参数)
Dim项目=TryCast(发送方、测试工具)
如果项目不是空的,那么
MessageBox.Show($“{item.NewProperty}{item.Text}”)
其他的
MessageBox.Show(DirectCast(发件人,ToolStripItem).Text)
如果结束
端接头
我相信您已经将
TestTool
类修改为@jmcilhinney注释为:

公共类测试工具
继承ToolStripMenuItem
次新
我的基地,新的
端接头
公共属性NewProperty作为字符串
末级

您是否应该继承
ToolStripMenuItem
而不是
ToolStripItem
?@jmchilinney谢谢,这很有帮助我尝试扩展菜单,但遇到类型问题,请参见编辑, please@jmcilhinney我花了几个小时进行调试,但没有得到任何运气:System.InvalidCastException:“无法将'System.Windows.Forms.ToolStripMenuItem'类型的对象强制转换为'WindowsApp1.TestTool'。
在'Dim TypeMenuItem As TestTool=men.DropDownItems.Add(“hh”)`上,当然是这样做的,因为该方法会返回这样的结果。您必须首先创建类型的实例,然后添加它。来自Microsoft的现有代码不会自动创建您类型的实例。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim men As New TestTool()
    men.Text = "showmeplease"
    men.Image = Nothing
    men.NewProperty = "BO"

    Dim men2 As New TestTool()
    men2.Text = "showmeplease2"
    men2.Image = Nothing
    men2.NewProperty = "BO2"

    tsiFruit.DropDownItems.Add(men)
    tsiFruit.DropDownItems.Add(men2)

    Dim TypeMenuItem As TestTool = men.DropDownItems.Add("hh")

    For Each mInfo As String In Moreinfo
        TypeMenuItem.DropDownItems.Add(mInfo, Nothing, AddressOf tsiType_Click)
    Next
    AddHandler men.Click, AddressOf tsiType_Click
    AddHandler men2.Click, AddressOf tsiType_Click
End Sub

Private p_Moreinfo As List(Of String)
Public Property Moreinfo() As List(Of String)
    Get

        Dim test As New List(Of String)
        test.Add("A")
        test.Add("B")
        Return test
    End Get
    Set(ByVal value As List(Of String))
        p_Moreinfo = value
    End Set
End Property