Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to display a treeview created in a seperate class and display it on a treeview on the main form?

Posted on 2011-10-14
8
Medium Priority
?
203 Views
Last Modified: 2012-05-12
Hi Experts,
I have been struggling with this for days now, i have created a class called project, which contains a private object called index, which in turn contains a treeview.
i also have created a treeview on the main form.
when the project is created in the main form, its constructor instantiates the the index class object which in turn creates a new treeview and adds afew nodes to it. I want to then make the treeview on the form equal and display that treeview (the one created in the project).
i cant seem to get this right. I have attached a bit of the simplified code in the hope you could help me see where im going wrong.
Regards
A

class Project
    {
        private Index index;

        public Project()
        {
            index = new Index();
        }

        public Index GetIndex()
        {
            return index;
        }
    }

class Index
    {
        private TreeView treeView;
        private TreeNode root;

        public Index()
        {
            treeView = new TreeView();
            root = new TreeNode("Node1");
        }

        public TreeView GetTreeView()
        {
            return treeView;
        }

        public TreeNode GetRootNode()
        {
            return root;
        }
    } 

public partial class Form1 : Form
    {
        private Project project;

        public Form1()
        {
            InitializeComponent();
        }

        private void newProjectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            project = new Project();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            treeView1 = project.GetIndex().GetTreeView();
        }  
    }

Open in new window

0
Comment
Question by:Amar_Hussain
  • 5
  • 3
8 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1000 total points
ID: 36969545
Instead of trying to replace treeView1, just omit it entirely and add your TreeView at run-time:

    this.Controls.Add(project.GetIndex().GetTreeView());
0
 

Author Comment

by:Amar_Hussain
ID: 36973676
ok but if i do this i have to manually set all the treeview properties via code to make sure the control displays correctly, isnt that more work? can we not just make the existing treeview control on the form reference the treeview in the project class? and display the data contained there?
0
 

Author Comment

by:Amar_Hussain
ID: 36973684
moving forward with your suggestion how would i then ensure the treeview fills the entire splitview container panel it is sitting in? and how would i adjust all the anchor settings etc?

 
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36973930
"... but if i do this i have to manually set all the treeview properties via code to make sure the control displays correctly"

That's the case already.  Any settings you had placed on the original treeView1 at design-time thru the IDE are lost when you replace it with the one from your class.

Why are you doing it this way to begin with?...

If you want to setup the TreeView at design-time (much easier right?), then I would suggest that you pass a reference to your existing TreeView into your class so that you can add nodes to it.  This way you aren't replacing the treeview at run-time.
0
 

Author Comment

by:Amar_Hussain
ID: 36974030
i want to create one instance of of a treeview in project (via index) and i wanted to encapsulate all tasks i wanted to do to the treeview in the index class, this way i can add remove nodes directly on the object. i thought this was good object orientated practice?
the project will also contain various other datasets etc...if i wanted to save the state of project, i would only have to serialise the project object (right?) and save it to a file, when i wanted to carry on working on the project at a later date i could deserialise the file and link it up to the various views...

maybe ive just gotten too used to VB but it seems very hard to link things up the way i want to...

i've upped the value of points as i think im being slightly dense and will probably need more stuff explained to me!
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 36974069
That's fine...but you should pass in a reference to the TreeView that is already on your form (instead of creating one dynamically).
0
 

Author Comment

by:Amar_Hussain
ID: 36974460
could you give me an example using the code given above, i've tried to do it but cant seem to get the treeview to display.
0
 

Author Closing Comment

by:Amar_Hussain
ID: 37078480
didnt really get a complete answer here....but the experts comments made me look at the problem from a different angle and i managed to solve the problem by cloning the treenode and then passing that copy to my project class.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Screencast - Getting to Know the Pipeline
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

810 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