Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Treeview > How to capture Escape key before/after the RadTreeView exits the edit mode?

Answered How to capture Escape key before/after the RadTreeView exits the edit mode?

Feed from this thread
  • Grigoriy avatar

    Posted on May 11, 2009 (permalink)

    Hello,

    Is there a way to capture the Escape key just before the RadTreeView controls exists the edit mode? We have a workflow in which a node can be added and the control is set to the edit mode, so the user is foced to update the node text. But if the user cancels editing by pressing Escape we want to remove the node and keep it if the user pressed Enter. Which we could do in the standard TreeView control by using the KeyPress event. But the RadTreeView does not fire the KeyPress event when it enters the edit mode. Is there a work around this issue or i will need to submit a feature request?

    Thanks,
    Grigoriy

    Reply

  • Answer Victor Victor admin's avatar

    Posted on May 13, 2009 (permalink)

    Hi Grigoriy,

    Thank you for your question.
     
    You can do this by creating a class which inherits from RadTreeView and a class which inherits from RadTreeViewTextEditor. In the new text editor you can override the OnKeyDown method and check for the escape key.
     
    Please see the sample code below, it demonstrates how this can be done.
     
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
            this.radTreeView1.AllowEdit = true;  
            this.radTreeView1.EditorRequired += new RadTreeView.EditorRequiredHandler(radTreeView1_EditorRequired);  
            this.radTreeView1.Edited += new EventHandler(radTreeView1_Edited);  
        }  
     
        void radTreeView1_Edited(object sender, EventArgs e)  
        {  
            if (this.textEditor.EscapeKeyPressed && !this.textEditor.Modified)  
            {  
                this.radTreeView1.SelectedNode.Remove();  
            }  
            this.textEditor.ResetEscapeKey();  
        }  
     
        MyTreeViewTextEditor textEditor = null;  
     
        void radTreeView1_EditorRequired(object sender, EditorRequiredEventArgs e)  
        {  
            this.textEditor = new MyTreeViewTextEditor();  
            e.Editor = this.textEditor;  
            this.textEditor.Value = this.radTreeView1.SelectedNode.Text;  
        }  
     
        public class MyTreeViewTextEditor : RadTreeViewTextEditor  
        {  
            protected override void OnKeyDown(KeyEventArgs e)  
            {  
                this.escapeKeyPressed = (e.KeyCode & Keys.Escape) == Keys.Escape;  
                base.OnKeyDown(e);  
            }  
     
            bool escapeKeyPressed = false;  
     
            public bool EscapeKeyPressed  
            {  
                get 
                {  
                    return this.escapeKeyPressed;  
                }  
            }  
     
            public void ResetEscapeKey()  
            {  
                this.escapeKeyPressed = false;  
            }  
        }  
     
    Please note that currently RadTreeView does not fire KeyPress or KeyDown because all input is redirected to the editor of the node you are editing.

    Do not hesitate to write again if you need further assistance.

     
    Sincerely yours,
    Victor
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.

    Reply

  • Grigoriy avatar

    Posted on May 13, 2009 (permalink)

    This approach worked. Thanks.

    Reply

  • V avatar

    Posted on Nov 21, 2011 (permalink)

    How could this be accomplished in asp.net?

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > Treeview > How to capture Escape key before/after the RadTreeView exits the edit mode?
Related resources for "How to capture Escape key before/after the RadTreeView exits the edit mode?"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]