Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How to add a user control inside a treeview control as a node
 

How to add a user control inside a treeview control as a node

I am having one user control (containing two combobox and one button) , I want to add
this control as a node in a treeview control , to be more precise now I want my user control
acting a node in the treeview control instead of treenode .
How should I achieve this ?

we can not extend treenode class because if I do this then I can not add Combobox and all to my control class . 
we can not inherit from treenode collection class as it is haing private constructor.

so if I want my control as a node in the treeview control then we can extend treeview class
and in that now I should have myControl's collection class which will ADD,REMOVE my control from a treeview control.

How t odo this ?

Thanks in advance 

Sunil Joshi.
MigrationUser 1  Thursday, December 02, 2004 4:14 AM
Hi Sunil,

I'm interested in the same thing - have you found a solution?

Rein
MigrationUser 1  Saturday, February 26, 2005 11:58 AM
Have you guys found a solution to this problem yet? I have the same problem too....
I want to add to my treeview node a user control containing a progress bar, a few buttons, a couple of comboboxes.

Thanks,
-Kamran Ansari
MigrationUser 1  Thursday, March 10, 2005 3:27 PM

this is the code for the treenodecollection, i dont know if it works, it probaly needs some tuning
maybe this can lead you in the good direction? hope this can help

namespace System.Windows.Forms
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Reflection;

    [Editor("System.Windows.Forms.Design.TreeNodeCollectionEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
    public class TreeNodeCollection : IList, ICollection, IEnumerable
    {
        // Methods
        <b>internal</b> TreeNodeCollection(TreeNode owner)
        {
            this.owner = owner;
        }

        public virtual TreeNode Add(string text)
        {
            TreeNode node1 = new TreeNode(text);
            this.Add(node1);
            return node1;
        }

        public virtual int Add(TreeNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (node.handle != IntPtr.Zero)
            {
                object[] objArray1 = new object[1] { node.Text } ;
                throw new ArgumentException(System.Windows.Forms.SR.GetString("OnlyOneControl", objArray1), "node");
            }
            TreeView view1 = this.owner.TreeView;
            if ((view1 != null) && view1.Sorted)
            {
                return this.owner.AddSorted(node);
            }
            this.owner.EnsureCapacity();
            node.parent = this.owner;
            node.index = this.owner.childCount++;
            this.owner.children[node.index] = node;
            node.Realize();
            if ((view1 != null) && (node == view1.selectedNode))
            {
                view1.SelectedNode = node;
            }
            return node.index;
        }

        public virtual void AddRange(TreeNode[] nodes)
        {
            if (nodes == null)
            {
                throw new ArgumentNullException("nodes");
            }
            TreeNode[] nodeArray1 = nodes;
            for (int num1 = 0; num1 < nodeArray1.Length; num1++)
            {
                TreeNode node1 = nodeArray1[num1];
                this.Add(node1);
            }
        }

        public virtual void Clear()
        {
            this.owner.Clear();
        }

        public bool Contains(TreeNode node)
        {
            return (this.IndexOf(node) != -1);
        }

        public void CopyTo(Array dest, int index)
        {
            if (this.owner.childCount > 0)
            {
                Array.Copy(this.owner.children, 0, dest, index, this.owner.childCount);
            }
        }

        public IEnumerator GetEnumerator()
        {
            return new WindowsFormsUtils.ArraySubsetEnumerator(this.owner.children, this.owner.childCount);
        }

        public int IndexOf(TreeNode node)
        {
            for (int num1 = 0; num1 < this.Count; num1++)
            {
                if (this[num1] == node)
                {
                    return num1;
                }
            }
            return -1;
        }

        public virtual void Insert(int index, TreeNode node)
        {
            if (node.handle != IntPtr.Zero)
            {
                object[] objArray1 = new object[1] { node.Text } ;
                throw new ArgumentException(System.Windows.Forms.SR.GetString("OnlyOneControl", objArray1), "node");
            }
            TreeView view1 = this.owner.TreeView;
            if ((view1 != null) && view1.Sorted)
            {
                this.owner.AddSorted(node);
            }
            else
            {
                if (index < 0)
                {
                    index = 0;
                }
                if (index > this.owner.childCount)
                {
                    index = this.owner.childCount;
                }
                this.owner.InsertNodeAt(index, node);
            }
        }

        public void Remove(TreeNode node)
        {
            node.Remove();
        }

        public virtual void RemoveAt(int index)
        {
            this[index].Remove();
        }

        bool ICollection.get_IsSynchronized()
        {
            return false;
        }

        object ICollection.get_SyncRoot()
        {
            return this;
        }

        int IList.Add(object node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (node is TreeNode)
            {
                return this.Add((TreeNode) node);
            }
            return this.Add(node.ToString()).index;
        }

        bool IList.Contains(object node)
        {
            if (node is TreeNode)
            {
                return this.Contains((TreeNode) node);
            }
            return false;
        }

        bool IList.get_IsFixedSize()
        {
            return false;
        }

        object IList.get_Item(int index)
        {
            return this[index];
        }

        int IList.IndexOf(object node)
        {
            if (node is TreeNode)
            {
                return this.IndexOf((TreeNode) node);
            }
            return -1;
        }

        void IList.Insert(int index, object node)
        {
            if (!(node is TreeNode))
            {
                throw new ArgumentException("node");
            }
            this.Insert(index, (TreeNode) node);
        }

        void IList.Remove(object node)
        {
            if (node is TreeNode)
            {
                this.Remove((TreeNode) node);
            }
        }

        void IList.set_Item(int index, object value)
        {
            if (!(value is TreeNode))
            {
                throw new ArgumentException("value");
            }
            this[index] = (TreeNode) value;
        }


        // Properties
        [EditorBrowsable(EditorBrowsableState.Advanced), Browsable(false)]
        public int Count
        {
            get
            {
                return this.owner.childCount;
            }
        }

        public bool IsReadOnly
        {
            get
            {
                return false;
            }
        }

        public virtual TreeNode this[int index]
        {
            get
            {
                if ((index < 0) || (index >= this.owner.childCount))
                {
                    throw new ArgumentOutOfRangeException("index");
                }
                return this.owner.children[index];
            }
            set
            {
                if ((index < 0) || (index >= this.owner.childCount))
                {
                    object[] objArray1 = new object[2] { "index", index.ToString() } ;
                    throw new ArgumentOutOfRangeException(System.Windows.Forms.SR.GetString("InvalidArgument", objArray1));
                }
                value.parent = this.owner;
                value.index = index;
                this.owner.children[index] = value;
                value.Realize();
            }
        }


        // Fields
        private TreeNode owner;
    }
}

MigrationUser 1  Tuesday, March 15, 2005 8:19 PM
Do You possibly know the other way around?

To put a tree control in a combo box?

Thanks in advance!!!
RafaMiranda  Tuesday, September 13, 2005 11:07 PM

You can use google to search for other answers

Custom Search

More Threads

• Dynamic form generation
• designer can't load form. Where is 'more information'?
• DatetimePicker in datagridview
• pls reply it is urgent! telephone digit limit in textbox, i cant use masked textbox
• <none>
• Creating DB2 Querry using .net
• My Mdi Parent is not opening
• Text Formatting in RichText Box
• Clicking an invisble button
• Disappearing Controls Visual Studio 2005