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

Validating Node Editing

6 Answers 222 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Brian
Top achievements
Rank 1
Brian asked on 04 Apr 2012, 03:27 PM
I was rather hoping that I could attach an event handler to the node editor Validating event  i.e.:

private void radTreeView1_EditorInitialized(object sender, TreeNodeEditorInitializedEventArgs e)
{
    e.Editor.Validating += new System.ComponentModel.CancelEventHandler(Editor_Validating);
}

However the Editor_Validating event handler doesn't appear to fire.


What I'm trying to achieve is to not allow the user to create a new node, or change the text of an existing node to the same value as any other node at the same level; and thus ensuring some level of uniqueness.

6 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 10 Apr 2012, 08:48 AM
Hello Brian,

Thank you for writing.

To validate the value of the currently edited Node, you can use ValueValidating event handler. Here is a sample:
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;
        }
 
        void treeView_ValueValidating(object sender, TreeNodeValidatingEventArgs e)
        {
            System.Console.WriteLine(e.NewValue);
        }
    }
}

I hope this helps.

Kind 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 >>
0
mtaber
Top achievements
Rank 1
answered on 11 Feb 2014, 05:03 AM
I've tried this, and in the treeView_ValueValidating function, after doing error checking, if you set e.Cancel = true; it doesn't send the Cancellation through to the treeView_Edited handler. However, if you start editing a node and hit the Escape button, it does trigger the cancel. Is there something else that needs to be done to make that work properly?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Feb 2014, 05:54 PM
Hello Mike,

Thank you for contacting Telerik Support.

It is important to note that the TreeViewElement.EditMode also takes part for the ValueValidating event. If you have TreeNodeEditMode.Text, the ValueValidating event is not going to fire at all, because in this mode when the user performs edit, the Text will be edited only. By default the EditMode is TextAndValue and the editor is initialized with the Value property of the RadTreeNode. Cancelling the ValueValidating event will not commit the changes. Could you please specify the exact steps how to reproduce the problem with cancelling the ValueValidating event or get back to me with a sample code snippet, which reproduces the specific behavior? Thank you in advance. I also need to know the exact version that you use. Thus, we would be able to investigate the specific case and suggest an appropriate solution if it is available. 

I am looking forward to your reply.

Regards,
Desislava
Telerik

Check out the new Telerik Platform - the only modular platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native apps. Register for the free online keynote and webinar to learn more about the Platform on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT).

0
mtaber
Top achievements
Rank 1
answered on 13 Feb 2014, 08:37 PM
Perhaps I wasn't clear. I'm using the default TreeViewElement.EditMode. It's set to TextAndValue.
When ValueValidating fires, inside of there I do error checking. If it fails to validate properly, I set e.Cancel = true which should theoretically cancel the editing.

When the treeView_Edited event then fires, c.Cancel for that event is NOT true. So it acts as though everything validated fine. My question is: What should I be doing inside of the ValueValidating function to be able to display an error message of some kind to the user and force them back to editing the node?

If the user is adding a node and they hit the Escape key, I can catch that in the treeView_Edited function as follows:

            if (e.Canceled)
            {
                System.Diagnostics.Debug.WriteLine("START: envTargetsTreeView_Edited Cancellation detected");
                // if we were adding a new node when editing was cancelled, simply delete the node
                // otherwise, we're simply going to end the editing mode
                if (((EnvTargetNodeData)envTargetsTreeView.SelectedNode.Tag).ObjectGuid == Guid.Empty)
                {
                    // remove the function pointer to prevent this function from being triggered when removing the node
                    envTargetsTreeView.Edited -= envTargetsTreeView_Edited;
                    envTargetsTreeView.SelectedNode.Remove();
                    envTargetsTreeView.Edited += new Telerik.WinControls.UI.TreeNodeEditedEventHandler(this.envTargetsTreeView_Edited);
                }
                return;
            }

But e.Cancelled is NOT true, even if in the ValueValidating function I tell it to cancel editing. There aren't any examples anywhere of how to implement validation for the tree nodes. What I'd like to do is display a message box of some kind letting them know the problem because it could be one of several problems. Then when they click "Ok" in the messagebox, take them back into editing mode. If they then cancel, it should revert back to the original value, or remove the node if it was being added.

This is using version 2012.2.912.40 if that helps at all.





0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 18 Feb 2014, 03:08 PM
Hello Mike,

Thank you for contacting us.

Cancelling the ValueValidating event via e.Cancel=true will keep the current node value. However, it will close the editor. This event uses TreeNodeValidatingEventArgs, but the Edited event uses TreeNodeEditedEventArgs, which e.Canceled is not related to the e.Cancel from the TreeNodeValidatingEventArgs. If your requirement is to keep the editor active when the validation fails, you can use a custom editor. Here is a sample solution, demonstrating how to enter edit mode if the validation fails:
public Form1()
{
    InitializeComponent();
 
    this.radTreeView1.AllowEdit = true;
    this.radTreeView1.TreeViewElement.EditMode = TreeNodeEditMode.TextAndValue;
 
    this.radTreeView1.ValueValidating += radTreeView1_ValueValidating;
    this.radTreeView1.ValidationError+=radTreeView1_ValidationError;
    this.radTreeView1.EditorRequired += radTreeView1_EditorRequired;
}
 
private void radTreeView1_ValidationError(object sender, EventArgs e)
{
    CustomTreeViewTextBoxEditor editor = this.radTreeView1.ActiveEditor as CustomTreeViewTextBoxEditor;
    if (editor!=null)
    {
        editor.OnValidationError(new ValidationErrorEventArgs(null, null));
    }
}
 
private void radTreeView1_EditorRequired(object sender, TreeNodeEditorRequiredEventArgs e)
{
    e.Editor = new CustomTreeViewTextBoxEditor();
}
 
private void radTreeView1_EditorInitialized(object sender, TreeNodeEditorInitializedEventArgs e)
{
}
 
public class CustomTreeViewTextBoxEditor : TreeViewTextBoxEditor
{
    bool validationFails = false;
 
    protected override void OnKeyDown(KeyEventArgs e)
    {
        TreeNodeElement nodeElement = this.OwnerElement as TreeNodeElement;
 
        if (e.KeyData == Keys.Enter)                       
        {
            nodeElement.TreeViewElement.EndEdit();
            if (validationFails)
            {
                DialogResult res = MessageBox.Show("Validation fails");
                if (res == System.Windows.Forms.DialogResult.OK)
                {
                    nodeElement.TreeViewElement.BeginEdit();
                }
            }
        }
        else
        {
            base.OnKeyDown(e);
        }
    }
 
    public override void OnValidationError(ValidationErrorEventArgs args)
    {
        validationFails = true;
        base.OnValidationError(args);
    }
}
 
private void radTreeView1_ValueValidating(object sender, TreeNodeValidatingEventArgs e)
{
    if (e.NewValue.ToString().Length == 1)
    {
        e.Cancel = true;
    }
}

Our Editing Nodes help article is quite useful about the editing process for the node.

Please do not hesitate to contact us if you have any additional questions.

Regards,
Desislava
Telerik
0
mtaber
Top achievements
Rank 1
answered on 25 Feb 2014, 03:30 PM
Got it. That helped to get it working. I think it was the Custom Editor that was the trick to getting it working because otherwise you don't really have a good way to hook into that event from the textbox. Thanks!
Tags
Treeview
Asked by
Brian
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
mtaber
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or