KeyDown support on popup RadCalendar for the RadDateTimePicker

1 Answer 95 Views
Calendar, DateTimePicker, TimePicker and Clock
Richard
Top achievements
Rank 1
Richard asked on 07 Jul 2021, 06:32 PM

All,

A simple question regarding the Calendar for the RadDateTimePicker using Microsoft WinForms.

The behavior I'm trying to fix is this:

I've put a KeyDown handler on the parent RadDateTimePicker that filters out backspace presses (since we don't want them in that control or any hosted controls in it). This appears to work fine (I can see the handler fire when this control has the focus). The RadDateTimePicker also has a hosted calendar control that can be accessed via a mouse click. When clicking on the UI element to get the popup Calendar, it properly pops up.. but when in the Calendar, if I now press the Backspace key, the Calendar popup disappears (this is the undesirable behavior).

I've tried this type of C# code to override keydown for the popup RadCalendar to get it to ignore Backspaces and it does not appear to work (I'm using exactly the same handler for now since I know it works):

            this.m_radDateTimePicker = new Telerik.WinControls.UI.RadDateTimePicker();

            //other initialization of the m_radDateTimePicker..

           Telerik.WinControls.UI.RadDateTimePickerCalendar calendarBehavior = (this.m_radDateTimePicker.DateTimePickerElement.GetCurrentBehavior() as Telerik.WinControls.UI.RadDateTimePickerCalendar);
            Telerik.WinControls.UI.RadCalendar calendar = calendarBehavior.Calendar as Telerik.WinControls.UI.RadCalendar;
            Telerik.WinControls.UI.RadCalendarElement calendarElement = calendar.CalendarElement as Telerik.WinControls.UI.RadCalendarElement;
            calendarElement.KeyDown += new System.Windows.Forms.KeyEventHandler(this.RadDateTimePicker_KeyDown);

 

I've tried this also on the RadCalendar (calendar.KeyDown += ...), to no effect.  Where should I place KeyHandler to get the popup Calendar to ignore Backspaces?

 

Thanks for your attention,

Richard

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 12 Jul 2021, 06:27 AM

Hello, Richard,

If I understand your requirement correctly, you want to handle the Backspace key when the popup calendar is opened and prevent the drop down from closing in this case.

The PopupManager internally handles the keyboard input and closes the drop down when the user presses Backspace. 

I have prepared a sample code snippet for your reference demonstrating how to prevent closing the drop down when the Backspace is hit:

        public RadForm1()
        {
            InitializeComponent(); 

            this.radDateTimePicker1.Opened += radDateTimePicker1_Opened;
        }

        private void radDateTimePicker1_Opened(object sender, EventArgs e)
        {
            RadDateTimePickerDropDown popup = PopupManager.Default.LastActivatedPopup as  RadDateTimePickerDropDown;
            popup.KeyPress -= popup_KeyPress;
            popup.KeyPress += popup_KeyPress;
        }

        private void popup_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.B || e.KeyChar == (char)Keys.Back)
            {
                e.Handled = true;
            }
        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Calendar, DateTimePicker, TimePicker and Clock
Asked by
Richard
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or