Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
I have a Treeview within a User Control. My plan was to populate the treeview from my main code. I don't know what should go into my user control to allow my main code to function to populate the tree.
Is there a simple way to reference the tree(Treeview1)?

Main code:
C#
SoftwareTabGroup.SoftwareTabControl[] SWtab = new SoftwareTabGroup.SoftwareTabControl[10];

SWtab[MainTabIndex] = new SoftwareTabGroup.SoftwareTabControl();


I want to access the tree like this?
C#
SWtab[MainTabIndex].SoftwareTree.Nodes.Clear();
SWtab[MainTabIndex].SoftwareTree.Nodes.Add(xxxxxxxx));



My User Control Code:
C#
namespace SoftwareTabGroup
{
    public partial class SoftwareTabControl : UserControl
    {
        public TreeView SoftwareTree
        {
            // what goes here???
            get
            {
              return TreeView1;
            }
            set
            {
                TreeView1 = value;
            }

        }

        public RichTextBox SoftwareComment
        {
            get
            {
                return SWComment;
            }
            set
            {
                SWComment = value;
            }
        }

        public SoftwareTabControl()
        {
            InitializeComponent();
        }
    }
}


Thanks,
Gary
Posted

1 solution

Yes, that should work: if you must do this, then I would make the setter private:
C#
public TreeView SoftwareTree
{
    get { return TreeView1; }
    private set { TreeView1 = value;}
}
Normally, I would not recommend making form components available as properties. If you only need to allow a subset of TreeView functionality, then I would prefer to expose public methods ClearTree, and AddTree instead, and keep the implementation of the control internal, and private. This means that you can change how your control works internally, without ffecting anything outside.

If you do need to expose a lot of TreeView functions, it may be more reliable to expose the whole tree. I still don't like it much though!
 
Share this answer
 
Comments
Gary TR 4-Mar-11 15:50pm    
Thanks for the answer.
Can you give me a code example of what you mean by ClearTree, AddTree?
OriginalGriff 4-Mar-11 15:52pm    
How about you try guessing? It's not exactly complex!
Hint:
ClearTree: think SoftwareTree.Nodes.Clear()
AddTree: think SoftwareTree.Nodes.Add(xxxxxxxx)

Anything leap to mind? :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900