This is a migrated thread and some comments may be shown as answers.

How to add button in every node?

5 Answers 807 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Stanley
Top achievements
Rank 1
Stanley asked on 23 Apr 2012, 04:57 PM
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.

5 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 24 Apr 2012, 04:49 PM
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
0
Richard Slade
Top achievements
Rank 2
answered on 25 Apr 2012, 12:51 PM
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
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 25 Apr 2012, 02:00 PM
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
0
Stanley
Top achievements
Rank 1
answered on 26 Apr 2012, 02:14 AM
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 


0
Stefan
Telerik team
answered on 26 Apr 2012, 03:05 PM
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 >>
Tags
Treeview
Asked by
Stanley
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Stanley
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or