Inheriting from RadMaskedEditBox control

1 Answer 103 Views
MaskedEditBox
Carl
Top achievements
Rank 1
Iron
Iron
Iron
Carl asked on 24 May 2023, 07:04 PM

Never did this before, but I need to make a custom change to a control that will affect all instances of it throughout the application. If the user clicks in a masked edit control at any position and the control is empty, the cursor should jump to position 1. It works fine when I use the event handler of the control directly like this:

 private void mtbHomePhone_Click(object sender, EventArgs e)
        {
            if (mtbHomePhone.Value?.ToString()?.Length == 0)
            {
                mtbHomePhone.SelectionStart = 1;
                mtbHomePhone.SelectionLength = 0;
            }
        }

 

I'm trying to inherit from the control like this (using MyMaskedEdit in the designer.cs in place of RadMaskedEditBox) but the event is not firing. What am I doing wrong?

using Telerik.WinControls.UI;

namespace VisionUI.Common
{
    public class MyMaskedEdit : RadMaskedEditBox
    {
        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);

            if (this.Value?.ToString()?.Length == 0)
            {
                this.SelectionStart = 1;
                this.SelectionLength = 0;
            }
        }
    }
}

 

Thanks

Carl

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 26 May 2023, 11:39 AM

Hi, Carl,

RadMaskedEditBox internally hosts the standard MS TextBox. The Click event at control level is fired internally but the mouse interaction is actually handled by the hosted TextBox. That is why when you create a derivative of the control, its OnClick method is not called.

The suitable approach here is to use the GotFocus event at control level, or override the OnGotFocus method in the derivative:

        public class MyMaskedEdit : RadMaskedEditBox
        {
            protected override void OnGotFocus(EventArgs e)
            {
                base.OnGotFocus(e);
            }
           
        }

Off topic, the following article demonstrates how to inherit the theme style as well: https://docs.telerik.com/devtools/winforms/telerik-presentation-framework/inherit-themes-from-radcontrols-derivatives 

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.

Carl
Top achievements
Rank 1
Iron
Iron
Iron
commented on 26 May 2023, 12:47 PM | edited

Dess

Thanks but I tried this approach. It does fire the event but it does not position the cursor the way Click does. I'm trying to do this without creating a custom control but it may not be possible.

        protected override void OnGotFocus(EventArgs e)

        {
            base.OnGotFocus(e);

            if (this.Value?.ToString()?.Length == 0)
            {
                this.SelectionStart = 1;
                this.SelectionLength = 0;
            }
        }

Ideas?

Carl

Dess | Tech Support Engineer, Principal
Telerik team
commented on 30 May 2023, 10:15 AM

Hi, Carl, 

It is possible to put some delay for the selection and now it should work in a similar way like clicking the editable part:

        public class MyMaskedEdit : RadMaskedEditBox
        {
            Timer t = new Timer();
            protected override void OnGotFocus(EventArgs e)
            {
                base.OnGotFocus(e);

                  t = new Timer();
                t.Interval = 100;
                t.Tick += T_Tick;
                t.Start();
            }

            private void T_Tick(object sender, EventArgs e)
            {
                t.Stop();
                if (this.Value?.ToString()?.Length != 0)
                {
                    this.SelectionStart = 1;
                    this.SelectionLength = 0;
                }
            }
        }

I hope this will fit your case.

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