Use Tab Key to select next property?

13 posts, 0 answers
  1. Daniel
    Daniel avatar
    19 posts
    Member since:
    Nov 2010

    Posted 22 May 2012 Link to this post

    Hi

    Is it possible to set the focus to the next [index++ in current sortorder] or to a named propertyfield when pressing Tab-Key in Editing-Mode. It seems that the Preview Key only works on Labels or Fields with Readonly Attribute.
    private void radPropertyGridPAZ_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        switch (e.KeyCode)      
        {
            case Keys.Tab:
                switch (radPropertyGridPAZ.SelectedGridItem.Name) 
                {
                    case "JobNo":
                        radPropertyGridPAZ.Items["TaskNo"].Select(); 
                        break;
                }
                break;
        }
    }

    Thanks in advance :-)
    Daniel Kaelin
  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 25 May 2012 Link to this post

    Hi Daniel,

    Thank you for writing.

    To achieve the desired behavior you will have to subscribe to the PreviewKeyDown event of each editor. You can use the EditorInitialized event of RadPropertyGrid for the purpose. Here is an example for three of the editors:
    private void radPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
    {
      if (e.Editor is PropertyGridSpinEditor)
      {
        PropertyGridSpinEditor editor = (PropertyGridSpinEditor)e.Editor;
        ((BaseSpinEditorElement)editor.EditorElement).TextBoxItem.HostedControl.PreviewKeyDown += new PreviewKeyDownEventHandler(HostedControl_PreviewKeyDown);
      }
      else if (e.Editor is PropertyGridTextBoxEditor)
      {
        PropertyGridTextBoxEditor edior = (PropertyGridTextBoxEditor)e.Editor;
        ((BaseTextBoxEditorElement)edior.EditorElement).TextBoxItem.HostedControl.PreviewKeyDown +=new PreviewKeyDownEventHandler(HostedControl_PreviewKeyDown);
      }
      else if (e.Editor is PropertyGridDateTimeEditor)
      {
        PropertyGridDateTimeEditor editor = (PropertyGridDateTimeEditor)e.Editor;
        ((BaseDateTimeEditorElement)editor.EditorElement).TextBoxElement.TextBoxItem.HostedControl.PreviewKeyDown +=new PreviewKeyDownEventHandler(HostedControl_PreviewKeyDown);
      }
      //...other editors
    }

    I hope this will be useful. If you have further questions, I would be glad to help.

    All the best,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'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. Daniel
    Daniel avatar
    5 posts
    Member since:
    Sep 2012

    Posted 17 Sep 2012 Link to this post

    Hi,

    How do I set the items to be in edit mode after the user push the tab key ?
    If I use the Select() method it will only put the item into selected mode.
    I have tried using BeginEdit(), it will set the item to edit mode but it will automatically set the item to end edit and set it back to selected mode.

    Regards,
    daniel
  5. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 20 Sep 2012 Link to this post

    Hello Daniel,

    Thank you for writing back.

    Please, find attached an example project where I have implemented your scenario. When closing and opening an item for edit within the PreviewKeyDown the event is fired twice. To overcome this the property grid KeyDown event is used to unsubscribe from the event.

    I hope this will help. If you have further questions, do not hesitate to write back.
     
    All the best,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  6. Daniel
    Daniel avatar
    5 posts
    Member since:
    Sep 2012

    Posted 25 Sep 2012 Link to this post

    I tried your code but modified the SomeClass properties into strings, as my codes require a string property.
    You have to press tab twice to actually edit the content (cursor is inside the box and waiting key press).
    After I tab once, the textbox in the property grid is indeed in edit mode, but when I tried to input using key press, no character can be inputted.

    Can you tell me what's wrong with it ?

    Regards,
    Daniel
  7. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 27 Sep 2012 Link to this post

    Hi Daniel,

    Thank you for writing.

    This behavior is caused by the fact that we use the standard TextBox control inside ours. The standard text box has a special behavior for the Tab key and if its properties Multiline and AcceptsTab are not set to true, it does not fire the KeyDown event. This is preventing us to achieve your scenario properly. To work around this, you can simply simulate the second tab press by sending a message to the application. I have modified the project in accordance to the above and your change of the type of the properties.

    Should you need further assistance, feel free to contact us.

    Greetings,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
  8. Kristoffer
    Kristoffer avatar
    158 posts
    Member since:
    Dec 2012

    Posted 23 Apr 2014 Link to this post

    I added a Button and a TextBox to Form1. Now the tabbing does not work. Suggestions?
  9. Dimitar
    Admin
    Dimitar avatar
    1414 posts

    Posted 23 Apr 2014 Link to this post

    Hi Kristoffer,

    Thank you for writing.

    To achieve the desired behavior and to handle the tab pressing properly you can create a custom RadPropertyGrid class and override the ProcessDialogKey method. For example: 
    public class MyPropertyGrid : RadPropertyGrid
    {
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Tab)
            {
                int index = this.Items.IndexOf((PropertyGridItem)this.SelectedGridItem);
     
                if (!(index < 0 || index >= this.Items.Count - 1))
                {
                    this.SelectedGridItem = this.Items[++index];
                    this.BeginEdit();
                    return true;
                }
                else
                {
                    return base.ProcessDialogKey(keyData);
                }
                
            }
            else
            {
                return base.ProcessDialogKey(keyData);
            }
     
        }
        public override string ThemeClassName
        {
            get
            {
                return typeof(RadPropertyGrid).FullName;
            }
        }
    }

    I hope this helps. Should you have any other questions do not hesitate to ask.
     
    Regards,
    Dimitar
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  10. Kristoffer
    Kristoffer avatar
    158 posts
    Member since:
    Dec 2012

    Posted 23 Apr 2014 in reply to Dimitar Link to this post

    Thanks. Almost there! Now we just need a way to tab over (skip) group items. Ideas? :)

    E.g.: "Group 1" has item "A" and "B". "Group 2" has item "C" and "D". At "B" I press TAB and expect it to start editing "C".
  11. Kristoffer
    Kristoffer avatar
    158 posts
    Member since:
    Dec 2012

    Posted 23 Apr 2014 Link to this post

    Got it working. Thanks for your help!

    protected override bool ProcessDialogKey(Keys keyData)
    {
        if (keyData == Keys.Tab || keyData == (Keys.Tab | Keys.Shift))
        {
            var gridItem = SelectedGridItem as PropertyGridItem;
            if (gridItem != null)
            {
                PropertyGridItem selectedItem = null;
     
                if (keyData == Keys.Tab)
                {
                    selectedItem = Items.ElementAtOrDefault(Items.IndexOf(gridItem) + 1);
     
                    if (selectedItem == null)
                    {
                        var currentGroup = Groups.SingleOrDefault(g => g.Name == gridItem.Category);
                        var nextGroup = Groups.ElementAtOrDefault(Groups.IndexOf(currentGroup) + 1);
     
                        if (nextGroup != null)
                        {
                            selectedItem = nextGroup.GridItems.FirstOrDefault();
                        }
                    }
                }
                else
                {
                    selectedItem = Items.ElementAtOrDefault(Items.IndexOf(gridItem) - 1);
     
                    if (selectedItem == null)
                    {
                        var currentGroup = Groups.SingleOrDefault(g => g.Name == gridItem.Category);
                        var previousGroup = Groups.ElementAtOrDefault(Groups.IndexOf(currentGroup) - 1);
     
                        if (previousGroup != null)
                        {
                            selectedItem = previousGroup.GridItems.LastOrDefault();
                        }
                    }
                }
     
                if (selectedItem != null)
                {
                    SelectedGridItem = selectedItem;
                    BeginEdit();
     
                    return true;
                }
            }
        }
         
        return base.ProcessDialogKey(keyData);
    }
  12. Dimitar
    Admin
    Dimitar avatar
    1414 posts

    Posted 24 Apr 2014 Link to this post

    Hello Kristoffer,

    I am glad that you have sorted this out and thank you for sharing your solution with the community. I am sure someone will benefit from it.

    Do not hesitate to contact us if you have other questions.
     
    Regards,
    Dimitar
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  13. Moe
    Moe avatar
    5 posts
    Member since:
    Oct 2009

    Posted 10 Sep 2014 Link to this post

    So based on the above solution, I am able to tab to the next control. The issue I am having, tab is jumping from control to another because I have Sorting and Grouping on. The question I have is how can I get the 'items' property after applying sorting and grouping? so when tab is clicked the next item in the propertygrid is selected sequentially .
  14. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 15 Sep 2014 Link to this post

    Hello Mohamed,

    Thank you for writing.

    You can iterate through the sorted/categorized properties items by pressing Tab key. For this purpose you should use a PropertyGridTraverser which traverses the items in a sorted/categorized order. Here is a sample code snippet:
    public class MyPropertyGrid : RadPropertyGrid
    {
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Tab)
            {
                if (this.SelectedGridItem != null)
                {
                    PropertyGridTraverser traverser = new PropertyGridTraverser(this.PropertyGridElement.PropertyTableElement);
      
                    while (traverser.MoveNext() && traverser.Current != this.SelectedGridItem)
                    {
                    }
      
                    traverser.MoveNext();
      
                    this.SelectedGridItem = traverser.Current;
                    this.BeginEdit();
      
                    return true;
                }
                else
                {
                    return base.ProcessDialogKey(keyData);
                }
            }
            else
            {
                return base.ProcessDialogKey(keyData);
            }
        }
      
        public override string ThemeClassName
        {
            get
            {
                return typeof(RadPropertyGrid).FullName;
            }
        }
    }

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top
UI for WinForms is Visual Studio 2017 Ready