Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
SoftwareTabGroup.SoftwareTabControl[] SWtab = new SoftwareTabGroup.SoftwareTabControl[10];
 
SWtab[MainTabIndex] = new SoftwareTabGroup.SoftwareTabControl();
 
I want to access the tree like this?
SWtab[MainTabIndex].SoftwareTree.Nodes.Clear();
SWtab[MainTabIndex].SoftwareTree.Nodes.Add(xxxxxxxx));
 

My User Control Code:
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 4-Mar-11 9:39am
Gary TR400

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Yes, that should work: if you must do this, then I would make the setter private:
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!
  Permalink  
Comments
Gary TR at 4-Mar-11 15:50pm
   
Thanks for the answer.
Can you give me a code example of what you mean by ClearTree, AddTree?
OriginalGriff at 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)

  Print Answers RSS
0 OriginalGriff 6,769
1 Sergey Alexandrovich Kryukov 6,338
2 DamithSL 5,408
3 Manas Bhardwaj 4,816
4 Maciej Los 4,195


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 4 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100