Solved

Large Treeview navigation menu

Posted on 2009-05-15
10
773 Views
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
                MainPanel.Controls.Add(AddCustCtrl)
                AddCustCtrl.Show()

            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.

Thanks!
0
Comment
Question by:garethh86
  • 6
  • 4
10 Comments
 
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.
0
 
LVL 7

Author Comment

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

Thanks
0
 
LVL 21

Accepted Solution

by:
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

MainPanel.Controls.Add(form)

form.Show()

Open in new window

0
 
LVL 7

Author Comment

by:garethh86
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'.

and

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

Any ideas?

Thanks
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 24400324
Sounds like you declared

Dim handle as EventHandler

instead of

Dim handle as ObjectHandle
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 7

Author Comment

by:garethh86
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.

on

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!
0
 
LVL 7

Author Comment

by:garethh86
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=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

I've tried importing AddCustomer on the form but produces same error.
0
 
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"
0
 
LVL 7

Author Comment

by:garethh86
ID: 24410601
Hi

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.

Thanks
0
 
LVL 7

Author Comment

by:garethh86
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now