Custom node editor - combobox

3 posts, 1 answers
  1. Marek Kruk
    Marek Kruk avatar
    16 posts
    Member since:
    Mar 2009

    Posted 06 Aug 2012 Link to this post

    Hi,

    in my scenario when i add new node i need select name from commbobox or input new name. How to replace the current editor of the combobox control.

    Thank you so much
    Marek.
  2. Answer
    Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 08 Aug 2012 Link to this post

    Hello Marek,

    Thank you for writing.

    Please take a look at our Demo application > TreeView > Manipulate data > Editing, for example of custom editors in RadTreeView. Here is a code snippet of example:
    using System;
    using System.Data;
    using System.Drawing;
    using Telerik.QuickStart.WinControls;
    using Telerik.WinControls;
    using Telerik.WinControls.UI;
     
     
    namespace Telerik.Examples.WinControls.TreeView.Editing
    {
        /// <summary>
        /// Form code
        /// </summary>
        public partial class Form1 : ExamplesForm
        {
            public Form1()
            {
                InitializeComponent();
                this.SelectedControl = this.radTreeView1;
            }
     
            private void LoadData()
            {
                DataTable table = new DataTable();
                table.Columns.Add("ID", typeof(int));
                table.Columns.Add("ParentID", typeof(int));
                table.Columns.Add("PropertyName", typeof(string));
                table.Columns.Add("PropertyValue", typeof(PropertyHelper));
     
                this.radPanel1.PanelElement.PanelText.ForeColor = Color.White;
     
                table.Rows.Add(0, null, "Panel", null);
                table.Rows.Add(1, 0, "Text", new PropertyHelper(radPanel1, "Text"));
                table.Rows.Add(2, 0, "Appearance", null);
                table.Rows.Add(3, 0, "Shape", null);
                table.Rows.Add(4, 2, "Fore color", new PropertyHelper(radPanel1.PanelElement.PanelText, "ForeColor"));
                table.Rows.Add(5, 2, "Back color", new PropertyHelper(radPanel1.PanelElement.PanelFill, "BackColor"));
                table.Rows.Add(6, 2, "Border color", new PropertyHelper(radPanel1.PanelElement.PanelBorder, "ForeColor"));
                table.Rows.Add(7, 2, "Border width", new PropertyHelper(radPanel1.PanelElement.PanelBorder, "Width"));
                table.Rows.Add(8, 3, "Corner radius", new PropertyHelper(radPanel1.PanelElement, "Shape"));
     
                this.radTreeView1.ChildMember = "ID";
                this.radTreeView1.ParentMember = "ParentID";
                this.radTreeView1.DisplayMember = "PropertyValue";
                this.radTreeView1.DataSource = table;
            }
     
            protected override void OnLoad(EventArgs e)
            {
                this.AutoScroll = false;
     
                base.OnLoad(e);
                LoadData();
                this.radTreeView1.NodeFormatting += new TreeNodeFormattingEventHandler(radTreeView1_NodeFormatting);
                this.radTreeView1.ValueChanging += new TreeNodeValueChangingEventHandler(radTreeView1_ValueChanging);
                this.radTreeView1.Editing += new TreeNodeEditingEventHandler(radTreeView1_Editing);
                this.radTreeView1.EditorInitialized += new TreeNodeEditorInitializedEventHandler(radTreeView1_EditorInitialized);
                this.radTreeView1.EditorRequired += new RadTreeView.EditorRequiredHandler(radTreeView1_EditorRequired);
                this.radTreeView1.ItemHeight = 27;
                this.radTreeView1.AllowAdd = false;
                this.radTreeView1.AllowRemove = false;
                this.radTreeView1.ExpandAnimation = ExpandAnimation.None;
                this.radTreeView1.AllowDefaultContextMenu = true;
                this.radTreeView1.ExpandAll();
            }
     
            void radTreeView1_Editing(object sender, TreeNodeEditingEventArgs e)
            {
                PropertyHelper helper = e.Node.Value as PropertyHelper;
                if (helper == null)
                {
                    e.Cancel = true;
                }
            }
     
            protected override void OnSizeChanged(EventArgs e)
            {
                base.OnSizeChanged(e);
                if (this.radPanel4 != null)
                {
                    int width = this.ClientSize.Width;
                    int height = this.ClientSize.Height;
     
                    height -= this.radPanel4.Height + this.radPanel3.Height;
                    height /= 2;
     
                    this.radPanel3.Location = new Point((width - this.radPanel4.Width) / 2, height);
                    this.radPanel4.Location = new Point((width - this.radPanel4.Width) / 2, height + this.radPanel3.Height);
                }
            }
     
            private void radCheckBoxAllowEdit_ToggleStateChanged(object sender, StateChangedEventArgs args)
            {
                this.radTreeView1.AllowEdit = args.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On;
            }
     
            private void radTreeView1_NodeFormatting(object sender, TreeNodeFormattingEventArgs args)
            {
                DataRowView rowView = (DataRowView)args.Node.DataBoundItem;
                string propertyName = (string)rowView["PropertyName"];
                object propertyValue = rowView["PropertyValue"];
     
                if (propertyValue != DBNull.Value)
                {
                    if (args.NodeElement.Children.Count == 4)
                    {
                        LightVisualElement descriptionElement = new LightVisualElement();
                        descriptionElement.StretchHorizontally = false;
                        descriptionElement.Font = ((LightVisualElement)args.NodeElement.Children[3]).Font;
                        descriptionElement.TextAlignment = ContentAlignment.MiddleLeft;
                        args.NodeElement.Children.Insert(3, descriptionElement);
                    }
     
                    ((LightVisualElement)args.NodeElement.Children[3]).Text = propertyName + ":  ";
                    args.NodeElement.ContentElement.ForeColor = Color.Blue;
                    args.NodeElement.ContentElement.Text = propertyValue.ToString();
                }
                else
                {
                    if (args.NodeElement.Children.Count == 5)
                    {
                        args.NodeElement.Children.RemoveAt(3);
                    }
                    args.NodeElement.ContentElement.Text = "<html><b>" + propertyName;
                    args.NodeElement.ContentElement.ForeColor = Color.Black;
                }
            }
     
            private void radTreeView1_ValueChanging(object sender, TreeNodeValueChangingEventArgs e)
            {
                PropertyHelper helper = (PropertyHelper)e.OldValue;
     
                if (helper.ValueType == typeof(System.Single))
                {
                    helper.Value = Convert.ToSingle(e.NewValue);
                }
                else if (helper.ValueType == typeof(Color))
                {
                    helper.Value = Color.FromName(Convert.ToString(e.NewValue));
                }
                else if (helper.ValueType == typeof(ElementShape))
                {
                    helper.Value = new RoundRectShape(Convert.ToInt32(e.NewValue));
                }
                else
                {
                    helper.Value = e.NewValue;
                }
     
                e.NewValue = helper;
            }
     
            private void radTreeView1_EditorInitialized(object sender, TreeNodeEditorInitializedEventArgs e)
            {
                TreeViewDropDownListEditor editor = e.Editor as TreeViewDropDownListEditor;
                if (editor != null)
                {
                    RadDropDownListElement listElement = (RadDropDownListElement)editor.EditorElement;
                    listElement.DropDownStyle = RadDropDownStyle.DropDownList;
                    if (listElement.Items.Count == 0)
                    {
                        listElement.Items.Add(new RadListDataItem("White", "White"));
                        listElement.Items.Add(new RadListDataItem("Black", "Black"));
                        listElement.Items.Add(new RadListDataItem("Gray", "Gray"));
                        listElement.Items.Add(new RadListDataItem("Red", "Red"));
                        listElement.Items.Add(new RadListDataItem("Blue", "Blue"));
                        listElement.Items.Add(new RadListDataItem("Green", "Green"));
                        listElement.Items.Add(new RadListDataItem("Yellow", "Yellow"));
                    }
                    listElement.SelectedValue = ((PropertyHelper)e.Node.Value).ToString();
                }
            }
     
            private void radTreeView1_EditorRequired(object sender, TreeNodeEditorRequiredEventArgs e)
            {
                if (e.Node.Value == DBNull.Value)
                {
                    return;
                }
     
                PropertyHelper helper = (PropertyHelper)e.Node.Value;
                if (helper != null)
                {
                    if (helper.ValueType == typeof(System.Single) || helper.ValueType == typeof(ElementShape))
                    {
                        e.EditorType = typeof(TreeViewSpinEditor);
                    }
                    else if (helper.ValueType == typeof(string))
                    {
                        e.EditorType = typeof(TreeViewTextBoxEditor);
                    }
                    else if (helper.ValueType == typeof(Color))
                    {
                        e.EditorType = typeof(TreeViewDropDownListEditor);
                    }
                }
            }
        }
    }


    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Windows.Forms;
     
    namespace Telerik.WinControls.UI
    {
        /// <summary>
        /// Represents a spin editor.
        /// </summary>
        [ToolboxItem(false)]
        public class TreeViewSpinEditor : BaseSpinEditor
        {
            #region Overrides
     
            protected override void OnKeyDown(KeyEventArgs e)
            {
                BaseSpinEditorElement editorElement = this.EditorElement as BaseSpinEditorElement;
     
                TreeNodeElement treeNodeElement = this.OwnerElement as TreeNodeElement;
                if (treeNodeElement != null)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.Return:
                            if (e.Modifiers != Keys.Control)
                            {
                                editorElement.Validate();
                                treeNodeElement.TreeViewElement.EndEdit();
                            }
                            break;
     
                        case Keys.Escape:
                            treeNodeElement.TreeViewElement.CancelEdit();
                            break;
     
                        case Keys.Delete:
                            if (selectionLength == editorElement.TextBoxItem.TextLength)
                            {
                                editorElement.Text = null;
                            }
                            break;
                    }
                }
            }
     
            protected override void OnKeyUp(KeyEventArgs e)
            {
                BaseSpinEditorElement editorElement = this.EditorElement as BaseSpinEditorElement;
     
                TreeNodeElement treeNodeElement = this.OwnerElement as TreeNodeElement;
                if (treeNodeElement != null)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.Right:
                            if ((RightToLeft && selectionStart == 0) || (!RightToLeft && selectionStart == editorElement.Text.Length))
                            {
                                editorElement.Validate();
                            }
                            break;
     
                        case Keys.Left:
                            if ((RightToLeft && selectionStart == editorElement.Text.Length) || (!RightToLeft && selectionStart == 0 && selectionLength == 0))
                            {
                                editorElement.Validate();
                            }
                            break;
                    }
                }
            }
     
            public override void OnLostFocus()
            {
                TreeNodeElement treeNode = this.OwnerElement as TreeNodeElement;
                if (treeNode != null &&
                    treeNode.IsInValidState(true) &&
                    !treeNode.ElementTree.Control.Focused &&
                    !treeNode.ElementTree.Control.ContainsFocus &&
                    !treeNode.TreeViewElement.IsPerformingEndEdit)
                {
                    treeNode.TreeViewElement.EndEdit();
                }
            }
     
            #endregion
        }
    }

    using System.Windows.Forms;
     
    namespace Telerik.WinControls.UI
    {
        public class TreeViewTextBoxEditor : BaseTextBoxEditor
        {
            #region Overrides
     
            protected override void OnKeyDown(KeyEventArgs e)
            {
                TreeNodeElement nodeElement = this.OwnerElement as TreeNodeElement;
                if (nodeElement != null)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.Escape:
                            nodeElement.TreeViewElement.CancelEdit();
                            break;
     
                        case Keys.Enter:
                            nodeElement.TreeViewElement.EndEdit();
                            break;
     
                        case Keys.Up:
                            if (!this.Multiline || (selectionLength == 0 && isAtFirstLine))
                            {
                                nodeElement.TreeViewElement.EndEdit();
                                nodeElement.TreeViewElement.ProcessKeyDown(e);
                            }
                            break;
     
                        case Keys.Down:
                            if (!this.Multiline || (selectionLength == 0 && isAtLastLine))
                            {
                                nodeElement.TreeViewElement.EndEdit();
                                nodeElement.TreeViewElement.ProcessKeyDown(e);
                            }
                            break;
                    }
                }
            }
     
            protected override void OnLostFocus()
            {
                TreeNodeElement treeNode = this.OwnerElement as TreeNodeElement;
                if (treeNode != null &&
                    treeNode.IsInValidState(true) &&
                    !treeNode.ElementTree.Control.Focused &&
                    !treeNode.ElementTree.Control.ContainsFocus &&
                    !treeNode.TreeViewElement.IsPerformingEndEdit)
                {
                    treeNode.TreeViewElement.EndEdit();
                }
            }
     
            #endregion
        }
    }

    using System.Windows.Forms;
    using Telerik.WinControls.Design;
     
    namespace Telerik.WinControls.UI
    {
        /// <summary>
        /// Represents a DropDownList editor.
        /// </summary>
        [RadToolboxItem(false)]
        public class TreeViewDropDownListEditor : BaseDropDownListEditor
        {
            #region Overrides
     
            protected override void OnKeyDown(KeyEventArgs e)
            {
                BaseDropDownListEditorElement editorElement = this.EditorElement as BaseDropDownListEditorElement;
     
                this.selectionStart = editorElement.SelectionStart;
     
                TreeNodeElement nodeElement = this.OwnerElement as TreeNodeElement;
                if (nodeElement != null)
                {
                    if (e.KeyCode == Keys.Enter && e.Modifiers != Keys.Control)
                    {
                        nodeElement.TreeViewElement.EndEdit();
                    }
                    else if (e.KeyCode == Keys.Escape)
                    {
                        nodeElement.TreeViewElement.CancelEdit();
                        e.Handled = true;
                    }
                    else if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
                    {
                        e.Handled = true;
                    }
                    else if (e.KeyCode == Keys.Down && e.Modifiers == Keys.Alt)
                    {
                        ((RadDropDownListElement)this.EditorElement).ShowPopup();
                        e.Handled = true;
                    }
                }
            }
     
            public override void OnValueChanged()
            {
                base.OnValueChanged();
     
                if (!((BaseDropDownListEditorElement)this.EditorElement).IsPopupOpen)
                {
                    TreeNodeElement element = this.OwnerElement as TreeNodeElement;
                    if (element != null && element.TreeViewElement != null && element.TreeViewElement.IsEditing)
                    {
                        element.TreeViewElement.EndEdit();
                    }
                }
            }
     
            protected override void OnLostFocus()
            {
                base.OnLostFocus();
     
                TreeNodeElement treeNode = this.OwnerElement as TreeNodeElement;
                if (treeNode != null &&
                    treeNode.IsInValidState(true) &&
                    !treeNode.ElementTree.Control.Focused &&
                    !treeNode.ElementTree.Control.ContainsFocus &&
                    !treeNode.TreeViewElement.IsPerformingEndEdit)
                {
                    treeNode.TreeViewElement.EndEdit();
                }
            }
     
            #endregion
        }
    }

    I hope this helps.

    Greetings,
    Julian Benkov
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Marek Kruk
    Marek Kruk avatar
    16 posts
    Member since:
    Mar 2009

    Posted 08 Aug 2012 Link to this post

    Thanks a lot
    Marek
Back to Top