Copy toolstrip items to menustrip

ManicCW

Well-known member
Joined
Jul 21, 2005
Messages
428
Location
Mostar
Programming Experience
Beginner
Hi I want to copy all items from toolstrip to menustrip. i have a sub that raises click event and does some work:

VB.NET:
    Private Sub Naljepnice_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
    tbMIL.Click, tbMPU.Click, tbMU.Click, tbMOP.Click, tbMKD.Click, tbMKDD.Click, tbR.Click, tbRS.Click, _
    tbNEZG.Click, tbNEZGU.Click, tbNEZGZ.Click, tbZIVS.Click

        Dim mn As ToolStripMenuItem
        mn = CType(sender, ToolStripItem)
        Select Case mn.Name.ToString
            Case "tbNMKDD", "mnNMKDD"
                CreateNew("Naljepnica", "MKD-D")
            Case Else
                CreateNew("Naljepnica", mn.Name.ToString.Remove(0, 2))
        End Select

    End Sub

code to copy items:

VB.NET:
    Private Sub frmInterface_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.mnNaljepnice.DropDownItems.Clear()
        Dim tb As ToolStripMenuItem
        For Each tb In Me.tbNaljepnice.DropDownItems
            Dim mn As New ToolStripMenuItem
            mn = tb
            mn.Name = tb.Name.ToString.Replace("tb", "mn")
            mn.Text = tb.Text
            Me.mnNaljepnice.DropDownItems.Add(mn.Text)
            [B]AddHandler mn.Click, AddressOf Naljepnice_Click[/B]
        Next
    End Sub

now i have a problem here. how do i attach the same event to menu? i have tried bolded part above but it does not work. i have no expirience with events so any help is great. tnx.
 
Please explain what doesn't work (do you get an error?). You should be able to use AddHandler to assign an EventHandler to an event as long as the EventHandler signatures are the same and in this case they are.
I don't see any problem with the code but it's somewhat hard to follow because of the non-english language and the strange naming conventions used.
 
I have commented your quoted code:
ManicCW said:
code to copy items:

VB.NET:
    Private Sub frmInterface_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.mnNaljepnice.DropDownItems.Clear()
        Dim tb As ToolStripMenuItem
        For Each tb In Me.tbNaljepnice.DropDownItems
            Dim mn As New ToolStripMenuItem
            mn = tb [COLOR=red]' <------------ here you change reference back to tb[/COLOR]
            mn.Name = tb.Name.ToString.Replace("tb", "mn") [COLOR=red]' <--- so here you actually change tb property value[/COLOR]
            mn.Text = tb.Text [COLOR=red]'<--- same here[/COLOR]
            Me.mnNaljepnice.DropDownItems.Add(mn.Text) [COLOR=red]' <--- here you add a new item with specified text, [/COLOR]
[COLOR=red]        ' but don't catch the return of Add method, which is the new ToolStripItem created[/COLOR]
            [B]AddHandler mn.Click, AddressOf Naljepnice_Click [COLOR=red]' <--- still working with tb instead of new 'mn' [/COLOR][/B]
[B][COLOR=red]          ' ...which results in adding handler to same tb item again[/COLOR][/B]
        Next
    End Sub
Here is how you should have done it:
VB.NET:
    Private Sub frmInterface_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.mnNaljepnice.DropDownItems.Clear()
        Dim tb As ToolStripMenuItem
        Dim mn As ToolStripMenuItem
        For Each tb In Me.tbNaljepnice.DropDownItems
            mn = Me.mnNaljepnice.DropDownItems.Add(tb.Text) 
            mn.Name = tb.Name.ToString.Replace("tb", "mn") 
            AddHandler mn.Click, AddressOf Naljepnice_Click 
        Next
    End Sub

Also moved thread to more appropriate forum Menus/toolbars.
 
thanks. my mistake. i thought something is wrong with event. i'll try it, but not until monday, i left it on work :)

p.s. why are my naming conventions strange? i allways use tb for toolbar item, mn for menu item, txt fot textbox and so on. maybe this is strange: MIL, MPU, MU but that is just document name that i need to print on click :)
 
Back
Top