如何以实用的方式访问工具条也以编程方式定义的点击事件

问题描述

我在表单编码中定义了工具条和工具条按钮。它的工作正常。我想知道如何访问每个按钮的点击事件。

当我运行表单时,ToolStrip 也会启用。但是当我点击 TooStripButtons 时它没有访问。

变量声明

Dim tsToolStrip As ToolStrip

Dim tsbNew,_
    tsbedit,_
    tsbDelete,_
    tsbSave,_
    tsbPrint,_
    tsbCancel As ToolStripButton

Dim tssSepe01,_
    tssSepe02,_
    tssSepe03 As ToolStripSeparator

工具条设置

Sub ToolStripSetup()
    
        ' ToolStrip Inizializing
        tsToolStrip = New ToolStrip
        ' ToolStrip Seperators Inizializing
        tssSepe01 = New ToolStripSeparator
        tssSepe02 = New ToolStripSeparator
        tssSepe03 = New ToolStripSeparator
        ' ToolStrip Buttons Inizializing
        tsbNew = New ToolStripButton
        tsbedit = New ToolStripButton
        tsbDelete = New ToolStripButton
        tsbSave = New ToolStripButton
        tsbPrint = New ToolStripButton
        tsbCancel = New ToolStripButton

        With tsbNew
            .Name = "tsbNew"
            .Image = My.Resources.NewFile_16x
            .Text = "&New"
            .displayStyle = ToolStripItemdisplayStyle.ImageAndText
            .TextimageRelation = TextimageRelation.ImageBeforeText
            .Enabled = False
        End With

        With tsbedit
            .Name = "tsbedit"
            .Image = My.Resources.Edit_16x
            .Text = "&Edit"
            .displayStyle = ToolStripItemdisplayStyle.ImageAndText
            .TextimageRelation = TextimageRelation.ImageBeforeText
            .Enabled = False
        End With

        With tsbDelete
            .Name = "tsbDelete"
            .Image = My.Resources.DeleteDatabase_16x
            .Text = "&Delete"
            .displayStyle = ToolStripItemdisplayStyle.ImageAndText
            .TextimageRelation = TextimageRelation.ImageBeforeText
            .Enabled = False
        End With

        With tsbSave
            .Name = "tsbSave"
            .Image = My.Resources.Save_16x
            .Text = "&Save"
            .displayStyle = ToolStripItemdisplayStyle.ImageAndText
            .TextimageRelation = TextimageRelation.ImageBeforeText
            .Enabled = False
        End With

        With tsbPrint
            .Name = "tsbPrint"
            .Image = My.Resources.Print_16x
            .Text = "&Print"
            .displayStyle = ToolStripItemdisplayStyle.ImageAndText
            .TextimageRelation = TextimageRelation.ImageBeforeText
            .Enabled = False
        End With

        With tsbCancel
            .Name = "tsbCancel"
            .Image = My.Resources.action_Cancel_16xLG
            .Text = "&Cancel"
            .displayStyle = ToolStripItemdisplayStyle.ImageAndText
            .TextimageRelation = TextimageRelation.ImageBeforeText
        End With

        ' Adding Buttons To ToolStrip
        With tsToolStrip
            .Name = "tsToolStrip"
            .gripStyle = ToolStripgripStyle.Hidden
            .RenderMode = ToolStripRenderMode.System
            With .Items
                .Add(tsbNew) ' New Button
                .Add(tsbedit) ' Edit Button
                .Add(tsbDelete) ' Delete Button
                .Add(tssSepe01)
                .Add(tsbSave) ' Save Button
                .Add(tssSepe02)
                .Add(tsbPrint) ' Print Button
                .Add(tssSepe03)
                .Add(tsbCancel) ' Cancel Button
            End With
        End With

        Controls.Add(tsToolStrip)

    
End Sub

我在上面发布了我的示例代码。任何人都可以帮助我如何做到这一点

解决方法

感谢回答

这就是我完成编码的方式

Dim WithEvents tsbNew,_
    tsbEdit,_
    tsbDelete,_
    tsbSave,_
    tsbPrint,_
    tsbCancel As ToolStripButton



Private Sub tsbCancel_Click(sender As Object,e As EventArgs) Handles tsbCancel.Click
    
        FormInizalization()
    
End Sub