• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

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

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
Amar_Hussain
Asked:
Amar_Hussain
  • 5
  • 3
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Instead of trying to replace treeView1, just omit it entirely and add your TreeView at run-time:

    this.Controls.Add(project.GetIndex().GetTreeView());
0
 
Amar_HussainAuthor Commented:
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
 
Amar_HussainAuthor Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"... 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
 
Amar_HussainAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Amar_HussainAuthor Commented:
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
 
Amar_HussainAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now