Large Treeview navigation menu

Posted on 2009-05-15
Last Modified: 2012-05-07
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.

Question by:garethh86
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
LVL 21

Expert Comment

by:Craig Wagner
ID: 24399584
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.

Author Comment

ID: 24399675
The treeview nodes are manually added, there will be no dynamic data added at runtime, its intirely static.

LVL 21

Accepted Solution

Craig Wagner earned 500 total points
ID: 24400103
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.


Author Comment

ID: 24400237
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?

LVL 21

Expert Comment

by:Craig Wagner
ID: 24400324
Sounds like you declared

Dim handle as EventHandler

instead of

Dim handle as ObjectHandle

Author Comment

ID: 24400370
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!

Author Comment

ID: 24400451
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.
LVL 21

Expert Comment

by:Craig Wagner
ID: 24406080
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"

Author Comment

ID: 24410601

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.


Author Comment

ID: 24411855
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.

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Introduction to Processes

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question