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

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

13 Answers 1866 Views
Buttons, RadioButton, CheckBox, etc
This is a migrated thread and some comments may be shown as answers.
Brandon
Top achievements
Rank 1
Brandon asked on 18 Apr 2012, 04:07 PM
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?

13 Answers, 1 is accepted

Sort by
0
Accepted
Ivan Petrov
Telerik team
answered on 21 Apr 2012, 02:45 PM
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 >>
0
Brandon
Top achievements
Rank 1
answered on 27 Apr 2012, 03:26 PM
It worked like a charm. Thank you Ivan!
0
Afraei
Top achievements
Rank 1
answered on 21 Feb 2013, 11:51 AM
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
0
Ivan Petrov
Telerik team
answered on 26 Feb 2013, 09:17 AM
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.
0
Afraei
Top achievements
Rank 1
answered on 26 Feb 2013, 09:41 AM
Thank you for your great help.

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

Regards.
0
hans
Top achievements
Rank 1
answered on 11 May 2017, 08:29 AM
But with this 'MyCheckBox' solution ToggleStateChanged does not seem to work anymore
0
Dimitar
Telerik team
answered on 11 May 2017, 09:14 AM
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.
0
hans
Top achievements
Rank 1
answered on 11 May 2017, 09:33 AM

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);
        }
    }
}

0
Dimitar
Telerik team
answered on 11 May 2017, 09:49 AM
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.
0
hans
Top achievements
Rank 1
answered on 11 May 2017, 09:51 AM

version 2015.3.930.40

toggle with mouse

0
Dimitar
Telerik team
answered on 11 May 2017, 10:26 AM
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.
0
hans
Top achievements
Rank 1
answered on 11 May 2017, 10:28 AM

thanks

does the problem not occur in the latest telerik version ?

0
Dimitar
Telerik team
answered on 11 May 2017, 10:33 AM
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.
Tags
Buttons, RadioButton, CheckBox, etc
Asked by
Brandon
Top achievements
Rank 1
Answers by
Ivan Petrov
Telerik team
Brandon
Top achievements
Rank 1
Afraei
Top achievements
Rank 1
hans
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or