Treeview menu?

Joined
Nov 13, 2008
Messages
6
Programming Experience
3-5
I'm relatively new to .NET and I'm looking to create a simple desktop application with a treeview object on the left side and some configurable options on the right, not unlike a typical website (think circa 1998 web page with frames, left side has links and the right displays a page with text).

I do not know where to begin other than the treeview object. I'm not sure how to make it so that when an item in the treeview menu is selected it changes what is seen to the right of it. Can anyone help me out? I just need some very basic instruction as to where to begin.
 
Handle the AfterSelect event of the TreeView, determine which node was selected from the e.Node property and then do whatever is appropriate. I think AfterSelect is the default event, so you can create an event handler just by double-clicking the control.
 
I've already gotten that far. Should I create panels and then add controls to them and then show() and hide() the panel depending upon which node is selected in the treeview?
 
It would have been nice if you'd told us that in the first place. Were we just to guess that part, even though all you said you knew was to add a TreeView?

What you should do in the details area depends wholy on what you need to show. How many different nodes are there in the TreeView? Do they all use the same layout in the details section, are they all different or are some the same and some different? How much information needs to be displayed in each case and in what form?

You might be able to use the same single set of controls in all cases. You might have to use a different set in every case. You might be able to reuse the same set for more than one node. If you need to change an entire set of controls then you should create a UserControl for each set, then you can add and remove them as a single unit.
 
No, you weren't supposed to guess. I figured it out after I posted. Every node will require a totally different set of controls to be displayed. There will be about 15-20 nodes. This is all going to be for an application that edits a configuration for another app, just to give you all an idea of what I'm talking about.

*edit*

Okay, after tinkering around with the UserControl class I think it's what I need. Thank you. I probably wouldn't have figured it out on my own.
 
Last edited:
No, you weren't supposed to guess. I figured it out after I posted.
I see.
Every node will require a totally different set of controls to be displayed. There will be about 15-20 nodes. This is all going to be for an application that edits a configuration for another app, just to give you all an idea of what I'm talking about.
In that case a UserControl for each node would probably be the way to go. You define and design a UserControl much as you do a form. Once you've built your project you can then use them like any other controls. They will expose whatever properties and methods you declared.

You could manage them in a few slightly different ways.

You could simply leave them all on screen at the same time. The user will only see the top one and you can just call BringToFront to show a different one.

You might only have one on screen at any one time, removing and adding as needed.

In either case, each node could have its corresponding control stored in its Tag property to give you easy access. I'd certainly suggest not creating any control until its corresponding node was selected, otherwise you might create some controls unnecessarily.

Note that the first option is probably a little easier, although the second might provide slightly better performance in the UI.
 
Alright, so I have my treeview menu working mostly.

I'm using a switch statement on the form to decide which UserControl object to add to the form, and nothing is loaded until it's requested. I'm actually quite happy with the way that all is coming together. My issue now is that it does not look good at all. I gave up on the splitter, because I couldn't get the docking right or anything.

What I want exactly is something like windows explorer. You have the tree on the left and your folders on the right (of course instead of folders I want to display my UserControl objects) and everything is resizable and in its on container. How do I accomplish this?
 
You don't use the Splitter in VB 2008. You use the SplitContainer. That's probably what you meant but the name you used is actually something else that does a similar job but in a different way. Please use the correct names for things to avoid confusion.

It's really very easy. Add a SplitContainer to your form and set its Dock property to Fill. Add your TreeView to the left-hand panel and set its Dock property to Fill. Add a Panel to the right-hand panel and set its Dock property to Fill. Now, each time you need to to display a UserControl you just add it to the Panel and set its Dock property to Fill.
VB.NET:
Dim uc As New SomeUserControl

uc.Dock = DockStyle.Fill
Me.Panel1.Controls.Add(uc)
 
Hopefully this will be my last question about this. Everything is working fairly well, except for one thing. I'm using the split container and the panel on the left has the treeview control and the panel on the right holds my UserControl objects. The split container's dock is set to fill and so is the treeview's and before I add each UserControl to the panel I set the dock to fill. It's working well. The only problem is that when I maximize the form there is some ghosting. The old bottom and right edges of the UserControl object are still visible. If I just resize the form, then there is no problem. It only happens when I maximize the form.

Any ideas? I could handle the maximize event and show() or hide() the usercontrol, but that seems like a hack.
 
I've cleared it up by handling the resize event. I then test if the window state has changed and if it is maximized. If so, then I hide() then show() the UserControl object. It works, but it seems like a hack. If anyone has any ideas, let me know.
 
I'm not sure why but the UC seems not to be repainting properly. You should be hiding and showing it. You should just call its Refresh method to force it to repaint.
 
Back
Top