Treeview Adding a node using begin edit

2 posts, 0 answers
  1. Harshvardhan
    Harshvardhan avatar
    4 posts
    Member since:
    May 2012

    Posted 07 May 2012 Link to this post

    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

  2. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts
    Member since:
    Sep 2012

    Posted 10 May 2012 Link to this post

    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 >>

Back to Top