treeview - How to Show appropriate form When Clicking Tree view child node in VB.net -


i have tree view have 2 root(masters, transactions) node , each root have 1 child(see tree view in image)

masters->party master

transactions->order acceptance

i want display frmpartymaster.vb form when clicking party master(child node) , display frmorder.vb form when clicking order acceptance(child node)

i tried tree view after select event(see below code)

 private sub treeview1_afterselect(byval sender system.object, byval e system.windows.forms.treevieweventargs) handles treeview1.afterselect     each node treenode in me.treeview1.nodes         getchildren(node)     next node end sub  function getchildren(byval parentnode treenode) list(of string)     dim nodes list(of string) = new list(of string)     getallchildren(parentnode, nodes)     return nodes end function  sub getallchildren(byval parentnode treenode, byval nodes list(of string))     each childnode treenode in parentnode.nodes         nodes.add(childnode.text)         getallchildren(childnode, nodes)     next end sub 

above code returns childnode's names in return nodes variable

now looking

  • how use nodes variable values

  • and how show appropriate form when clicking child node(i know child node haven't click event)

  • tell me other efficient way found instead of afterselect event because loop through root node , child node each time

according code provided, not trying instantiate class depending on node have selected. doing adding nodes list, not needed...

this solution can add any class type form.

there few ways can accomplished, provided 1 way achieve.

imports system.reflection   public class form1  private const form_party_master string = "frmpartymaster" private const form_order string = "frmorder"   private sub form1_load(sender object, e eventargs) handles mybase.load     dim mnode new treenode {.text = "masters"}     mnode.nodes.add(new treenode {.text = "party masters", .tag = form_party_master})      dim transnode new treenode {.text = "transaction"}     transnode.nodes.add(new treenode {.text = "order/acceptance", .tag = form_order})      treeview1.nodes.addrange({mnode, transnode})  end sub  private sub treeview1_afterselect(sender object, e treevieweventargs) handles treeview1.afterselect     dim obj form             try         if e.node.tag isnot nothing             obj = ctype(activator.createinstance(type.gettype("windowsapplication5." & e.node.tag.tostring, false)), form)             if obj isnot nothing                 using obj                     obj.showdialog()                 end using             end if         end if     catch ex exception         'handle exception     end try end sub end class 

explanation

  1. create constants represent forms.
  2. the load routine populate data. notice use tag property of treenode, use instance of form need.
  3. the afterselect event used. see how create instance of whatever control need; no case or if statements.
  4. make sure change windowsapplication5 namespace be.

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -