vb.net - Add ContextMenuStipItem at Run time -


i'm working on little application run in system tray. there menustrip show when user right clicks on icon in tray. 1 of buttons opens form user can add new buttons strip. can create buttons when button added path gets set other buttons.

example: create new button name "documents". set path documents folder. works. add button named "c". set path "c:\". sets path , opens c:\, sets documents button "c:\" well.

there 2 text-boxes on form, 1 name , 1 path. name shows in menu, , path should open when user clicks button in menu.

private sub button1_click(sender object, e eventargs) handles button1.click     dim newitem new toolstripmenuitem     newitem.text = namebox.text     addhandler newitem.click, addressof buttonclick     contextmenustrip1.items.add(newitem)  end sub  private sub buttonclick(byval sender system.object, byval e eventargs)     process.start("explorer.exe", pathbox.text) end sub 

i understand happening, i'm not sure how fix it. setting buttons click event buttonclick, how create new sub each button handles own click event @ runtime?

pathbox.text returns current value of textbox.

instead, should add lambda expression , capture original value in closure:

dim path string = pathbox.text  addhandler newitem.click, sub     process.start("explorer.exe", path) end sub 

Comments