Public Static TreeView

G

Guest

Visual Studio 2003 .NET / C#

I have a treeview object on a form which acts as the main menu controller
for my application. the treeview is always in sight, and the form it is on
acts as the main container for the whole application. All other screens are
UserControls, which get added and removed from/to the main form, when the
user clicks an item on the tree view.

I thought the best way to handle navigating around would be to define the
treeview as public static, and I have a method in the main form called
ChangeScreen which is also public static. Then from any object in my project
I can just say MainMenu.ChangeScreen("Add Job"), and this will then change
the screen to the Add Job screen, and change the selected Node etc in my tree
view.

The problem is, every so often visual studio seems to just change the
definition of my treeview to just public, instead of public static. Then of
course i get build errors because I am referencing a non static object in my
static method (ChangeScreen). Anyone now why this? Or a better way of
making this Menu change function global?

Thanks Steve
 
F

Frans Bouma [C# MVP]

Steve said:
Visual Studio 2003 .NET / C#

I have a treeview object on a form which acts as the main menu
controller for my application. the treeview is always in sight, and
the form it is on acts as the main container for the whole
application. All other screens are UserControls, which get added and
removed from/to the main form, when the user clicks an item on the
tree view.

I thought the best way to handle navigating around would be to define
the treeview as public static, and I have a method in the main form
called ChangeScreen which is also public static. Then from any
object in my project I can just say MainMenu.ChangeScreen("Add Job"),
and this will then change the screen to the Add Job screen, and
change the selected Node etc in my tree view.

The problem is, every so often visual studio seems to just change the
definition of my treeview to just public, instead of public static.
Then of course i get build errors because I am referencing a non
static object in my static method (ChangeScreen). Anyone now why
this? Or a better way of making this Menu change function global?

Your treeview is an INSTANCE referenced by a form object, so it's not
preferable to have that static. (no use also, it's local to the form).

If you want to communicate with the treeview, do that through the
form, that's proper OO encapsulation.

I have a similar setup, I have a form with a treeview with a complete
project graph, and when someone selects something in a context menu on
a node, the code fires events which are handled by the mainform
(handled as in: re-routed to the proper code to handle/do the action).

As, after all, it's no different from MDI programming where the
mainform is the controller of the application and all actual forms are
childs inside that form, communicating with eachother and the rest of
the app THROUGH the mainform.

Frans

--
 
G

Guest

So how do I communicate with the tree view from within one of the child
forms(User Controls in my case) that sit in the main form? The user can
navigate to other menu options from within child forms, so I need a way of
firing off a menu item from within a child object.

Thanks

Steve
 
G

Guest

Think I have a solution:

I have a Form called MainForm. On this form is just a TreeView. MainForm
has a public method called ChangePage which recieves a string.

The TreeView has a structure like this:

-Main1
- Sub1a
-Main2
- Sub2a

I have 3 User Controls, SubForm1, SubForm2, SubForm1a. When I click on the
Sub1a treeview item, it creates an instance of SubForm1, and adds it to the
Controls collection of my MainForm. SubForm1a is a member of SubForm1's
controls.

SubForm1 has a button to change page to SubForm2. When the user clicks this
button it does this:

((MainForm)this.Parent).ChangePage("Sub2a");

this then calls the changePage method in my MainForm which in turn, removes
SubForm1 from the Controls collection, and adds SubForm2 instead.

Likewise SubForm1a, which is within SubForm1, has a button to do the same
thing, to do this my code says:

((MainForm)this.Parent.Parent).ChangePage("Sub2a");

This seems to work fine for me.

Thanks

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top