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

Treeview Adding a node using begin edit

1 Answer 183 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Harshvardhan
Top achievements
Rank 1
Harshvardhan asked on 07 May 2012, 03:11 PM
I have a functionality where i need to add nodes to the tree and get the name of the node form the user , in the node by using beginedit.
I have some custom validation that  i need to do , i am using the valueValidation event for doing that. once the validation is done , If the validation fails , i.e when i do e.cancel = true and validation error gets triggered , i want the new node added during the process to be removed. I tried doing it in validationerror event , but this is not allowed. please let me know how can i do this.

Regards
Harsh

1 Answer, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 10 May 2012, 02:48 PM
Hello Harshvardhan,

Thank you for writing.

You can use the combination of the Edited and the ValueValidting events to handle this behavior. Here is a sample application demonstrating this approach:
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace Lab.Tree
{
    public partial class TreeEditorValidating : MainForm
    {
        private RadTreeView treeView = new RadTreeView();
 
        public TreeEditorValidating()
        {
            InitializeComponent();
 
            treeView.Dock = DockStyle.Fill;
            treeView.Parent = this;
            treeView.BringToFront();
            treeView.AllowEdit = true;
 
            treeView.Nodes.Add("1");
            treeView.Nodes[0].Nodes.Add("11");
            treeView.Nodes[0].Nodes.Add("12");
            treeView.Nodes.Add("2");
            treeView.Nodes.Add("3");
 
            treeView.ValueValidating += treeView_ValueValidating;
            treeView.Edited += treeView_Edited;
        }
 
        bool removeLast = false;
        void treeView_Edited(object sender, TreeNodeEditedEventArgs e)
        {
            if (removeLast)
            {
                treeView.Nodes.Remove(e.Node);
            }
 
            removeLast = false;
        }
 
        void treeView_ValueValidating(object sender, TreeNodeValidatingEventArgs e)
        {
            if (removeLast)
            {
                return;
            }
 
            if (e.NewValue.ToString() != "MyValue")
            {
                removeLast = true;
                e.Cancel = true;
            }
        }
 
        protected override void OnButton1Click()
        {
            RadTreeNode node = treeView.Nodes.Add("new node");
            node.BeginEdit();
        }
    }
}

I hope this helps.

Regards,
Julian Benkov
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
Harshvardhan
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Share this question
or