如何为动态创建的上下文菜单条指定动态控件?

时间:2015-05-26 07:04:02

标签: vb.net

我创建了一个动态的contextmenustrip并为其添加了动态项。如何为其分配动态控件。例如

我已经添加了动态项目 " A1",:" A2"," A3" ......等等

每个值都有对流布局面板的控制 如果我点击A1,那么按钮应该从父面板移动到" A1"面板。如果我点击" A2"它必须去面板" A2"。流动面板的数量是动态的。 这个问题清楚了吗? 是否可以动态分配控件? 这是我到目前为止所做的代码

这里我创建了动态菜单并添加了项目

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        Dim pt As New ContextMenuStrip
        Dim pt1 As New ContextMenuStrip
        Me.ContextMenuStrip = pt
        pt.Name = "Cont1"
        For Each c As Control In FlowLayoutPanel1.Controls
            If TypeOf c Is FlowLayoutPanel Then
                array.Add(c.Name)
                AddHandler pt.Click, AddressOf contest
            End If
        Next
        Dim data As String
        For Each Data In array
            pt.Items.Add(data)
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

这里我想要控件哪里有问题

Private Sub contest(sender As Object, e As EventArgs)
    Dim pt As New ContextMenuStrip
    pt = CType(sender, ContextMenuStrip)
    MsgBox(pt.Name)
End Sub

2 个答案:

答案 0 :(得分:1)

您应该使用此方法:

Control.Controls.Add(control)

所以它就是例如:

panelA1.Controls.Add(button1)

编辑:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
    Dim pt As New ContextMenuStrip
    Dim pt1 As New ContextMenuStrip
    Me.ContextMenuStrip = pt
    pt.Name = "Cont1"
    For Each c As Control In FlowLayoutPanel1.Controls
        If TypeOf c Is FlowLayoutPanel Then
            array.Add(c.Name)
            'AddHandler pt.Click, AddressOf contest
        End If
    Next
    Dim data As String
    For Each Data In array
        Dim mui As New ToolStripMenuItem(data)
        AddHandler mui.Click, AddressOf contest
        pt.Items.Add(mui)
    Next
Catch ex As Exception
    MsgBox(ex.Message)
End Try
End Sub

然后在比赛中,ctype发送者到toolstripmenuitem而不是contextmenustrip,并根据你知道调用者的文字:

Private Sub contest(sender As Object, e As EventArgs)
    Dim mui As New ToolStripMenuItem
    mui = CType(sender, ToolStripMenuItem)
    MsgBox(mui.Text)
End Sub

答案 1 :(得分:0)

现在,这会创建动态的contextmenustrip,可以在用户表单中获取flowlayoutpanel,并将按钮从一个lowlayoutpanel移动到另一个

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        Dim pt As New ContextMenuStrip
        Dim pt1 As New ContextMenuStrip
        Me.ContextMenuStrip = pt
        pt.Name = "Cont1"
        For Each c As Control In FlowLayoutPanel1.Controls
            If TypeOf c Is FlowLayoutPanel Then
                array.Add(c.Name)
            End If
        Next
        Dim data As String
        For Each data In array
            Dim mui As New ToolStripMenuItem(data)
            AddHandler mui.Click, AddressOf contest
            pt.Items.Add(mui)
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
Private Sub contest(sender As Object, e As EventArgs)
    Dim mui As ToolStripItem
    Dim str As String
    mui = CType(sender, ToolStripItem)
    str = mui.Text
    Dim flo As FlowLayoutPanel = DirectCast(FlowLayoutPanel1.Controls(str), FlowLayoutPanel)
    MsgBox(mui.Text)
    flo.Controls.Add(Button1)
End Sub