override onkeydown in radmaskededitbox

1 Answer 113 Views
MaskedEditBox
Roh
Top achievements
Rank 1
Roh asked on 04 May 2021, 10:44 AM

Hi.  I want to bring a custom control that inherits from RasMaskedEditBox. But onKeyDown and OnKeyPress events do not fired.

public class myMask: Radmaskededitbox

{

protected override void OnKeyDown(KeyEventArgs e)

{

//My codes

}

}

 

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 05 May 2021, 01:36 PM

Hi, Roh,

RasMaskedEditBox internally hosts the standard MS TextBox. Hence, it handles the keyboard input. The following code snippet demonstrates how to handle the KeyDown event in the hosted control:

        public RadForm1()
        {
            InitializeComponent();

            this.radMaskedEditBox1.MaskedEditBoxElement.TextBoxItem.TextBoxControl.KeyDown+=TextBoxControl_KeyDown; 
        }
          
        private void TextBoxControl_KeyDown(object sender, KeyEventArgs e)
        {
            Console.WriteLine("KeyDown");
        }

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

Regards,
Dess | Tech Support Engineer, Sr.
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.

Roh
Top achievements
Rank 1
commented on 20 Aug 2021, 02:06 PM

Hi.
Thanks a lot for your reply.
I tried your sample code and I think it is incomplete or I did not ask my questions correctly.
Is it possible to give an accurate example that is practical?
I want to write an override function for the OnKeyDown event from a class inherited from the Radmaskededitbox class
Dess | Tech Support Engineer, Principal
Telerik team
commented on 23 Aug 2021, 08:39 AM

Hi, Roh, 

Since RadMaskedEditBox internally hosts the standard MS TextBox, the hosted control handles the keyboard input first. Then, the message is passed to the RadMaskedEditBox and RadMaskedEditBoxElement respectively. It is possible to suppress the keyboard's input from the hosted text box. Thus, you can implement your logic in the overridden OnKeyDown method from RadMaskedEditBox:
        public RadForm1()
        {
            InitializeComponent();

            this.radMaskedEditBox1.MaskedEditBoxElement.TextBoxItem.TextBoxControl.KeyDown += TextBoxControl_KeyDown; 
        }

        private void TextBoxControl_KeyDown(object sender, KeyEventArgs e)
        {
            //restricts user's input
            e.SuppressKeyPress = true; 
        }

        public class CustomMaksedEditBox : RadMaskedEditBox
        {
            public override void OnKeyDown(object sender, KeyEventArgs e)
            {
                //You can implement your logic here
                base.OnKeyDown(sender, e); 
            }

            public override string ThemeClassName  
            { 
                get 
                { 
                    return typeof(RadMaskedEditBox).FullName;  
                }
            }
        }

I believe that it would fit your custom scenario. However, if you are still experiencing any further difficulties, it would be greatly appreciated if you can provide more details about the exact goal that you are trying to achieve. Thus, we would be able to get better understanding of the precise case and think about a suitable solution. Thank you in advance.

Tags
MaskedEditBox
Asked by
Roh
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or