Checkbox: Disable the checking of checkbox when enter key is pressed.

14 posts, 1 answers
  1. Brandon
    Brandon avatar
    17 posts
    Member since:
    Sep 2011

    Posted 18 Apr 2012 Link to this post

    When the checkbox is focused and the enter key is pressed, the checkbox then gets checked.
    This is an undesired feature, and my client requested that the spacebar only will check and uncheck the checkbox.

    How do I change this?
  2. Answer
    Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 21 Apr 2012 Link to this post

    Hello Brandon,

    Thank you for writing.

    To change this behavior, you will have to create your own checkbox element, inheriting from RadCheckBoxElement and change the logic in the KeyDown event. You will also have to create your check box control, inheriting from RadCheckBox and override the CreateButtonElement to return your checkbox element. Here is a code snippet which demonstrates this:
    public class MyCheckBoxElement : RadCheckBoxElement
    {
      protected override void OnKeyDown(KeyEventArgs e)
      {
        if (e.KeyCode == Keys.Enter)
        {
          return;
        }
     
        base.OnKeyDown(e);
      }
     
      protected override Type ThemeEffectiveType
      {
        get { return typeof(RadCheckBoxElement); }
      }
    }
     
    public class MyCheckBox : RadCheckBox
    {
      protected override RadButtonElement CreateButtonElement()
      {
        return new MyCheckBoxElement();
      }
     
      public override string ThemeClassName
      {
        get
        {
          return typeof(RadCheckBox).FullName;
        }
      }
    }
    After you create the custom control and you build your project, it should appear in you toolbox. If it does not appear in the toolbox you can add a [ToolboxItem(true)] attribute to MyCheckBox class.

    I hope you will be this useful. If you have further questions, I would be glad to help.
     
    Regards,
    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. Brandon
    Brandon avatar
    17 posts
    Member since:
    Sep 2011

    Posted 27 Apr 2012 Link to this post

    It worked like a charm. Thank you Ivan!
  4. Afraei
    Afraei avatar
    18 posts
    Member since:
    Jun 2012

    Posted 21 Feb 2013 Link to this post

    Hi dear Ivan,

    I have created my custom CheckBox but can I ask you how to use this checkbox in a RadTreeNode?
    I couldn't do this by checkType!

    Your guide is appreciated in advance.
    Ragards
  5. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 26 Feb 2013 Link to this post

    Hello Afraei,

    Thank you for writing.

    In order to use your custom check box in a tree node you will have to create a custom tree node element and replace the default ones. To replace the check box inside a TreeNodeElement you can override the CreateToggleElement method. To make RadTreeView to use your node elements you can subscribe for the CreateNodeElement event. Here is an example:

    A custom check box:
    public class MyCheckBoxElement : TreeNodeCheckBoxElement
    {
        protected override void OnKeyDown(KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    return;
                }
     
                base.OnKeyDown(e);
            }
     
        protected override Type ThemeEffectiveType
            {
                get { return typeof(RadCheckBoxElement); }
            }
    }

    A custom tree node element:
    public class MyTreeNodeElement : TreeNodeElement
    {
        protected override RadToggleButtonElement CreateToggleElement()
        {
            if (this.Data.CheckType == CheckType.CheckBox)
            {
                return new MyCheckBoxElement();
            }
     
            return base.CreateToggleElement();
        }
     
        protected override Type ThemeEffectiveType
        {
            get { return typeof(TreeNodeElement); }
        }
    }

    And replacing the tree elements:
    this.radTreeView1.CreateNodeElement += radTreeView1_CreateNodeElement;
     
    private void radTreeView1_CreateNodeElement(object sender, CreateTreeNodeElementEventArgs e)
    {
        e.NodeElement = new MyTreeNodeElement();
    }

    I hope this will help. Feel free to write back with any further questions.

    Greetings,
    Ivan Petrov
    the Telerik team
    Q3'12 SP1 of RadControls for WinForms is out now. See what's new.
  6. Afraei
    Afraei avatar
    18 posts
    Member since:
    Jun 2012

    Posted 26 Feb 2013 Link to this post

    Thank you for your great help.

    Your kindly help is more appreciated if you provide me a sample for this solution.

    Regards.
  7. hans
    hans avatar
    10 posts
    Member since:
    Aug 2006

    Posted 11 May Link to this post

    But with this 'MyCheckBox' solution ToggleStateChanged does not seem to work anymore
  8. Dimitar
    Admin
    Dimitar avatar
    1780 posts

    Posted 11 May Link to this post

    Hi Hans,

    I have tested this with the latest version and it is working fine on my side. Could you please check the attached video and let me know what else I need to do in order to reproduce this?

    I am looking forward to your reply.
     
    Regards,
    Dimitar
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  9. hans
    hans avatar
    10 posts
    Member since:
    Aug 2006

    Posted 11 May in reply to Dimitar Link to this post

    This is my derived checkbox : it does not fire togglestatechanged event with my version of telerik

    using System;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;

    namespace Iap.WinApp.Controls.CheckBox
    {
        public class QCheckBoxTest : RadCheckBox
        {
            public override string ThemeClassName
            {
                get { return typeof (RadCheckBox).FullName; }
            }

            protected override RadButtonElement CreateButtonElement()
            {
                return new MyCheckBoxElementTest();
            }
        }

        public class MyCheckBoxElementTest : RadCheckBoxElement
        {
            protected override Type ThemeEffectiveType
            {
                get { return typeof (RadCheckBoxElement); }
            }

            protected override void OnKeyDown(KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    return;
                }

                base.OnKeyDown(e);
            }
        }
    }

  10. Dimitar
    Admin
    Dimitar avatar
    1780 posts

    Posted 11 May Link to this post

    Hello Hans,

    Which version of the suite are you currently using? And how you are changing the toggle state, with the mouse or the keyboard?

    I am looking forward to your reply.
     
    Regards,
    Dimitar
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  11. hans
    hans avatar
    10 posts
    Member since:
    Aug 2006

    Posted 11 May in reply to Dimitar Link to this post

    version 2015.3.930.40

    toggle with mouse

  12. Dimitar
    Admin
    Dimitar avatar
    1780 posts

    Posted 11 May Link to this post

    Hello Hans,

    I was able to reproduce this with the version that you are using. Here is how you can trigger the event:
    public class QCheckBoxTest : RadCheckBox
    {
        public override string ThemeClassName
        {
            get { return typeof(RadCheckBox).FullName; }
        }
     
        protected override RadButtonElement CreateButtonElement()
        {
            var el = new MyCheckBoxElementTest();
            el.ToggleStateChanged += El_ToggleStateChanged;
            return el;
        }
     
        private void El_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            this.OnToggleStateChanged(args);
        }
    }

    Should you have any other questions do not hesitate to ask.

    Regards,
    Dimitar
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  13. hans
    hans avatar
    10 posts
    Member since:
    Aug 2006

    Posted 11 May in reply to Dimitar Link to this post

    thanks

    does the problem not occur in the latest telerik version ?

  14. Dimitar
    Admin
    Dimitar avatar
    1780 posts

    Posted 11 May Link to this post

    Hi Hans,

    This working fine with the latest version.

    Regards,
    Dimitar
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top