0

i have created a dynamic contextmenustrip and added dynamic item to it. How do i assign dynamic controls to it. For example

i have added dynamic item as such "A1",:"A2","A3"... so on

each value has controls to the flow-layout panel if i click A1 then the button should be moved from parent panel to the "A1" panel. if i click "A2" it has to go to panel "A2". The no of flow panels are dynamic. Is this question clear? Is it possible to assign controls dynamically as well? Here is the code i did so far

Here i created dynamic menu and adding item

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

Here i want the controls where am having problem

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

2 Answers2

1

You should use this method:

Control.Controls.Add(control)

so it would be for example:

panelA1.Controls.Add(button1)

Edit:

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

Then in contest, ctype sender to toolstripmenuitem instead of contextmenustrip and based on the text you would know whos the caller:

Private Sub contest(sender As Object, e As EventArgs)
    Dim mui As New ToolStripMenuItem
    mui = CType(sender, ToolStripMenuItem)
    MsgBox(mui.Text)
End Sub
Keith Mifsud
  • 725
  • 5
  • 16
  • Thank you for the quick replay. But how do i find which panel is selected. I mean the context menu add's item dynamically and i don't know how many panels are there in the form. So how do i set controls for dynamic menu strip action – Sunil bhagavath May 26 '15 at 07:52
  • ok, you're going to have to change the load method... lemme post an answer – Keith Mifsud May 26 '15 at 07:58
  • hang on, do you want the context menu to be bound to each panel? if so in the load method instead of the addhandler set the `c.ContextMenuStrip = pt` and then check this post out http://stackoverflow.com/questions/2074898/how-do-you-get-the-control-that-was-clicked-to-open-a-contextmenustrip – Keith Mifsud May 26 '15 at 08:11
  • Thanks it worked.. Exactly what i was looking for.. One more question if dont mind me asking. Can the button be moved from one flow panel to another? – Sunil bhagavath May 26 '15 at 09:07
  • umm, yes but it must be moved manually as in, remove it from panel A and add it to panel B like `panelA.Controls.Remove(btn1) panelB.Controls.Add(btn1)` – Keith Mifsud May 26 '15 at 09:13
  • Yes it worked.with little modification it worked exactly as i wanted it to work thank you for your help.. Really appreciate it – Sunil bhagavath May 26 '15 at 10:12
0

Now this creates dynamic contextmenustrip witch gets the flowlayoutpanel avalable in the userforms and moves button from one lowlayoutpanel to the other

 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
  • I took my time to help you out and then you decide that your answer which is mostly my code, is a better answer than mine... thank you sir... much appreciated... – Keith Mifsud Jun 23 '15 at 06:25
  • @KeithMifsud I am So sorry for what happened. But i have no Idea how This had happened. Probality my brother or one of my friends did this by mistake. But i Promise You i didnt Do this. I Logged This site on and didnt logoff the site and went on vaccasion for the last couple of week. But sumthin Happened and i dont know what happened here. All my Reputation Is gone almost 12 points. Am so sorry but this isnt my doing i honestly didnt mean to hurt you. – Sunil bhagavath Jun 23 '15 at 10:57
  • Am sorry anyway. Look you posted this almost a month ago and think about it why would i change the status now. After all i was the one who gave the credit. Am sorry – Sunil bhagavath Jun 23 '15 at 10:59