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

6 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. UI for WinForms is Visual Studio 2017 Ready
  4. 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!
  5. 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
  6. 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.
  7. 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.
Back to Top
UI for WinForms is Visual Studio 2017 Ready