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

4 posts, 1 answers
  1. Grigoriy
    Grigoriy avatar
    7 posts
    Member since:
    Jun 2008

    Posted 11 May 2009 Link to this post

    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
  2. Answer
    Victor
    Admin
    Victor avatar
    1351 posts

    Posted 13 May 2009 Link to this post

    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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Grigoriy
    Grigoriy avatar
    7 posts
    Member since:
    Jun 2008

    Posted 13 May 2009 Link to this post

    This approach worked. Thanks.
  5. V
    V avatar
    1 posts
    Member since:
    Nov 2011

    Posted 21 Nov 2011 Link to this post

    How could this be accomplished in asp.net?
Back to Top