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

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

3 Answers 143 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Grigoriy
Top achievements
Rank 1
Grigoriy asked on 11 May 2009, 11:11 PM
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

3 Answers, 1 is accepted

Sort by
0
Accepted
Victor
Telerik team
answered on 13 May 2009, 04:39 PM
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.
0
Grigoriy
Top achievements
Rank 1
answered on 13 May 2009, 10:01 PM
This approach worked. Thanks.
0
V
Top achievements
Rank 1
answered on 22 Nov 2011, 05:14 AM
How could this be accomplished in asp.net?
Tags
Treeview
Asked by
Grigoriy
Top achievements
Rank 1
Answers by
Victor
Telerik team
Grigoriy
Top achievements
Rank 1
V
Top achievements
Rank 1
Share this question
or