Large Treeview navigation menu

Hi all

I'm developing an app which contains a large amount of data input screens, I've split these up into seperate forms which are then added as controls to a panel in the main form.

I've decided to use a TreeView control as a navigation menu to get to these different input forms. The only problem is the code needed to find out which treeview item was clicked and then display the form is quite bulky, for example:

        If e.Action = TreeViewAction.ByMouse Then
            If e.Node.FullPath = "Customers\Add Customer" Then
                Dim AddCustCtrl As New AddCustomer
                AddCustCtrl.TopLevel = False
                AddCustCtrl.Dock = DockStyle.Fill

            End If

        End If

Whats the best way of cutting down this code - can you give me an example of a function that would allow this to be done more simply. If not then am I best putting all of this inside a dll so it is seperated and makes the code on the main form more managable? Or is there an entirely different way of doing this that is easier?

The treeview has over 60 different items.

Who is Participating?
Craig WagnerConnect With a Mentor Software ArchitectCommented:
As you populate the TreeView, use the Tag property of each node to store the name of the class that should be created when the user clicks that node. The class name must be fully qualified (i.e. include the namespace).

You say that these are forms. I'm assuming from that that you mean they inherit from System.Windows.Forms.Form. In your code you would then do something like in the code snippet.

If your controls are not actually forms, you would have to change the way you cast the ObjectHandle, probably to something like Control.
Dim handle As ObjectHandle = _
           Activator.CreateInstance(Assembly.GetExecutingAssembly().FullName, e.Node.Tag.ToString())
Dim form As Form = CType(handle.Unwrap(), Form)
form.TopLevel = False
form.Dock = DockStyle.Fill

Open in new window

Craig WagnerSoftware ArchitectCommented:
I have a couple of ideas, but they depend on how you're originally populating the TreeView. If you could give me some idea how the TreeView is initially being populated I can see if either of my ideas would work in your case.
garethh86Author Commented:
The treeview nodes are manually added, there will be no dynamic data added at runtime, its intirely static.

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

garethh86Author Commented:
Hey I'm getting two errors with this:

Error      1      Value of type 'System.Runtime.Remoting.ObjectHandle' cannot be converted to 'System.EventHandler'.


Error      2      'Unwrap' is not a member of 'System.EventHandler'.

Any ideas?

Craig WagnerSoftware ArchitectCommented:
Sounds like you declared

Dim handle as EventHandler

instead of

Dim handle as ObjectHandle
garethh86Author Commented:
Argh, sorry, selected the wrong item in autocomplete. I'm getting this error at run time when clicking a node:

Object reference not set to an instance of an object.


Dim handle As Runtime.Remoting.ObjectHandle = _
           Activator.CreateInstance(Reflection.Assembly.GetExecutingAssembly().FullName, e.Node.Tag.ToString())

I've renamed the form to be the same as the node name, is this right?

Thanks for your help!
garethh86Author Commented:
Sorry, being stupid, the node didn't have a tag - now that i've added that Im getting the error:

Could not load type 'AddCustomer' from assembly 'SMS, Version=, Culture=neutral, PublicKeyToken=null'.

I've tried importing AddCustomer on the form but produces same error.
Craig WagnerSoftware ArchitectCommented:
I made some assumptions, and gave one caveat, in my original post:

1. I assumed the forms you are instantiating are in the same assembly as the code for the TreeView. If that is not the case then you'll have to change Assembly.GetExecutingAssembly().FullName to be the name of the assembly containing the form.

2. From my original post: "The class name must be fully qualified (i.e. include the namespace)." I don't know what your namespace is for the form, but assuming it is in the same assembly as the TreeView code, and assuming the namespace matches the assembly name, you should use "SMS.AddCustomer"
garethh86Author Commented:

They are both in the same assembly, I have tried imports SMS.AddCust but this makes no difference.

I've tried this in a new project with two forms, treeview and a panel - I still get the same error.

garethh86Author Commented:
Got it this way:

Dim newform = e.Node.Tag

            Dim AddForm As Form = CType(Reflection.[Assembly].GetEntryAssembly.CreateInstance( _
            My.Application.Info.AssemblyName & "." & newform), Form)

Thanks for putting me on the correct track though, this has cut several hundred lines of code own to 5 or 6 and alows me to manage what happens more.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.