How to add button in every node?

6 posts, 1 answers
  1. Stanley
    Stanley avatar
    32 posts
    Member since:
    Apr 2012

    Posted 23 Apr 2012 Link to this post

    Please see the picture in attachment.

    Can I add "Add Node" button in every node?
    Rather than, user choose >> right click >> click New >> key in.

    Is possible?
    Thank You.
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 24 Apr 2012 Link to this post

    Hello Stanley, 

    This could probably be improved, and I'm not sure if nodeformatting is the most efficient way to go, but this should give you a head start in trying to perform this functionality. 

    int _i = 1;
     
    public Form1()
    {
        InitializeComponent();
     
        this.radTreeView1.NodeFormatting += new TreeNodeFormattingEventHandler(radTreeView1_NodeFormatting);
        this.radTreeView1.Nodes.Add(new RadTreeNode("My First Node"));
    }
     
    void radTreeView1_NodeFormatting(object sender, TreeNodeFormattingEventArgs e)
    {
        if (e.NodeElement.Children.Count == 4)
        {
            RadButton button = new RadButton();
            button.Size = new Size(40, 22);
            button.Text = "Add Node";
            RadHostItem host = new RadHostItem(button);
            e.NodeElement.Children.Add(host);
            button.Tag = e.Node.Name;
            button.Click += new EventHandler(button_Click);
        }
    }
     
    void button_Click(object sender, EventArgs e)
    {
        RadTreeNode node = this.radTreeView1.GetNodeByName(((RadButton)sender).Tag.ToString());
        node.Nodes.Add(new RadTreeNode("A new node " + _i.ToString()));
        node.Expand();
        _i++;
    }

    Hope that helps
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 25 Apr 2012 Link to this post

    Hi again, 

    I'm working on something else for you that uses a custom node element. Will have something to you as soon as possible
    Richard
  5. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 25 Apr 2012 Link to this post

    Hi, 

    Ok - this is better and is much more effective. Please remember to mark as answer. Let me know if you need further help. 

    Form 
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls;
    using Telerik.WinControls.UI;
     
    namespace RadControlsWinFormsApp1
    {
        public partial class Form1 : Form
        {
     
     
            public Form1()
            {
                InitializeComponent();
     
                this.radTreeView1.CreateNodeElement += new CreateTreeNodeElementEventHandler(radTreeView1_CreateNodeElement);
                this.radTreeView1.ShowExpandCollapse = true;
                this.radTreeView1.LineStyle = TreeLineStyle.Solid;
                this.radTreeView1.Nodes.Add(new RadTreeNode("My First Node"));
                this.radTreeView1.Nodes.Add(new RadTreeNode("My Second Node"));
                this.radTreeView1.Nodes.Add(new RadTreeNode("My Third Node"));
                this.radTreeView1.AllowEdit = true;
            }
     
     
            private void radTreeView1_CreateNodeElement(object sender, CreateTreeNodeElementEventArgs e)
            {
                e.NodeElement = new HostNodeElement();;
            }
     
            
     
        }
    }

    HostNodeElement
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Telerik.WinControls.UI;
    using Telerik.WinControls;
    using System.Drawing;
     
    namespace RadControlsWinFormsApp1
    {
        public class HostNodeElement : TreeNodeElement
        {
     
     
            protected override void InitializeFields()
            {
                base.InitializeFields();
            }
     
            protected override void CreateChildElements()
            {
                base.CreateChildElements();
     
                RadButton button = new RadButton();
                button.Size = new Size(65, 15);
                button.Text = "Add Node";
                RadHostItem host = new RadHostItem(button);
                 
                button.Click += new EventHandler(button_Click);
                host.StretchHorizontally = false;
                host.StretchVertically = true;
     
                this.Children.Add(host);
            }
     
            void button_Click(object sender, EventArgs e)
            {
                RadTreeNode node = new RadTreeNode("New Node");
                this.Data.Nodes.Add(node);
                if (this.TreeViewElement.AllowEdit)
                {
                    node.BeginEdit();           
                }
                this.Data.Expand();
            }
     
            protected override Type ThemeEffectiveType
            {
                get { return typeof(TreeNodeElement); }
            }
        }
    }

    Richard
  6. Stanley
    Stanley avatar
    32 posts
    Member since:
    Apr 2012

    Posted 25 Apr 2012 Link to this post

    Hi Richard,

    Thanks for your help.
    That is what I need. ^^

    Could you help me another topic? haha
    See last reply.
    http://www.telerik.com/community/forums/winforms/combobox-and-listbox/treeview-in-combobox.aspx#2081450 


  7. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 26 Apr 2012 Link to this post

    Hello guys,

    I just want to mention that the custom node solution is the better approach for this scenario. Using it, instead of using RadButton and RadHostItem, you can use RadButtonElement.

    I hope that you find this information useful.
     
    Greetings,
    Stefan
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready