VB.Net 如何从 ContextMenuStrip 禁用 FlowLayoutPanel 中的控件并获取名称

How to disable and get name for control in FlowLayoutPanel from ContextMenuStrip in VB.Net

我的程序包含 FlowLayoutPanel 中的按钮。
我想在右键单击任何按钮时禁用它并单击 ContextMenuStrip 中的 "Disable"。

我的代码是:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 0 To 30
            Dim btn As New Button
            btn.Name = i
            btn.Text = i
            btn.ContextMenuStrip = ContextMenuStrip1
            FlowLayoutPanel1.Controls.Add(btn)
        Next
    End Sub

End Class

声明一个 public 变量以保持控制

    public ctrl as button = nothing

您可以通过将此代码放在鼠标上来创建右键单击...

If e.Button <> Windows.Forms.MouseButtons.Right Then Return
        Dim cms = New ContextMenuStrip

        ctrl = sender

        Dim item1 = cms.Items.Add("Disable")
        item1.Tag = 1
        AddHandler item1.Click, AddressOf Disable
end if 

在 diable sub 中你可以这样编码...

Private Sub Disable()
    ctrl.enabled = false
End Sub