vb.net - dynamically create a new tab with textbox, button in the tab with click and keypress events (AddHandler is what i cant get to work) -
i creating tabs , need 2 things work can't work. need addhandler
textbox.keypress
event , button.click
event. can make these things work outside of tabcontrol not in.
in example below text box , buttons have same name on tab another, thought might problem changing names between tabs not work. assume need more specific in addhandler part give tab name control. there logic in real code allow me give unique names each tab panel , controls, can't simple part work.
i left of things tried commented, tried lots , lots of other things.
public class form1 public sub addtab(tabpagename string) dim tabpage new tabpage tabpage.text = tabpagename tabpage.name = "tabpage1" 'real code has logic make sure names unique dim label1 new label dim txtcreator new textbox dim combox1 new combobox dim tabpagebutton2 new button tabpagebutton2.parent = tabpage label1.parent = tabpage txtcreator.parent = tabpage combox1.parent = tabpage label1.location = new point(10, 10) txtcreator.location = new point(150, 10) combox1.location = new point(300, 10) tabpagebutton2.location = new point(20, 40) label1.text = "creator" txtcreator.name = "txtcreator" 'fill comboboxes...this come database testing now. combox1.items.add("one") combox1.items.add("two") combox1.items.add("three") 'ok works should work db no problem. tabroleclass.tabpages.add(tabpage) end sub private sub form1_load(sender object, e system.eventargs) handles me.load addtab("first tab") addhandler controls("tabroleclass.tabpage1.tabpagebutton2").click, addressof tabpagebutton_click 'addhandler ctype(controls("tabpagebutton"), button).click, addressof tabpagebutton_click 'addhandler controls("tabpagebutton").click, addressof tabpagebutton_click addhandler ctype(controls("txtcreator"), textbox).keypress, addressof txtcreator_keypress 'the keypress call lookup end sub private sub tabpagebutton_click(sender system.object, e system.eventargs) 'handles tabpagebutton.click messagebox.show(tabroleclass.selectedtab.name.tostring) end sub private sub button1_click(sender system.object, e system.eventargs) handles button1.click addtab("second tab") tabroleclass.selectedindex = tabroleclass.tabcount - 1 'addhandler controls("tabroleclass.tabpage1.tabpagebutton2").click, addressof tabpagebutton_click 'addhandler ctype(controls("tabpagebutton"), button).click, addressof tabpagebutton_click 'addhandler controls("tabpagebutton").click, addressof tabpagebutton_click 'addhandler ctype(controls("txtcreator"), textbox).keypress, addressof txtcreator_keypress 'the keypress call lookup end sub private sub txtcreator_keypress(byval sender object, byval e system.windows.forms.keypresseventargs) 'handles txtcreator.keypress messagebox.show("keypress worked on " & tabroleclass.selectedtab.name.tostring) end sub end class
this confusing question , code cleaning, need add addhandler
code addtab
subroutine pointed out @plutonix:
public sub addtab(tabpagename string) dim tabpage new tabpage dim tabpagebutton new button dim txtcreator new textbox /.../ addhandler tabpagebutton.click, addressof tabpagebutton_click addhandler txtcreator.keydown, addressof txtcreator_keypress tabroleclass.tabpages.add(tabpage) end sub private sub tabpagebutton_click() messagebox.show(tabroleclass.selectedtab.name.tostring) end sub private sub txtcreator_keypress() messagebox.show("keypress worked on " & tabroleclass.selectedtab.name.tostring) end sub private sub form1_load(sender object, e system.eventargs) handles me.load addtab("first tab") end sub private sub button1_click(sender system.object, e system.eventargs) handles button1.click addtab("second tab") tabroleclass.selectedindex = tabroleclass.tabcount - 1 end sub
addhandler
works adding event handlers controls. this means each time event raised during runtime, new event handler handle event; everytime click tabpagebutton
associated event tabpagebutton_click
handle it.
therefore, need add handler once, preferably upon creation of control. there absolutely no need create them upon every single keypress, example. should event handlers on msdn.
hope helps!