Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1878
UltraMaskedEdit showing credit card expiry
posted

Hi,

maybe there is an easy solution for my purpose but I cannot find it. I want to have a text editor where the user may enter the expiry date of a credit card in format mm/yyyy. UltraMaskedEdit control already comes very close by setting the input mask to mm/yyyy but I want to give the user also the possibility to see what format he has to enter so ideally the text editor should display MM/YYYY when the text editor does not have the focus. I did a workaround by setting the text of the text editor to MM/YYYY when the form loads, in the Enter event I clear the text and set the input mask with mm/yyyy. In the Leave event I clear the input mask and set Text to MM/YYYY again if user has not input any data. This workaround works but I want to use databinding so it's getting a little bit clumsy so I'm asking if there is a better solution, maybe with another control?

Regards, Wolfgang

  • 469350
    Offline posted

    Hi Wolfgang,

    The only way I can think to do this would be to use a DrawFilter or a CreationFilter to change the display without changing the value. Here's a quick example.


        internal class DateMaskCreationFilter : IUIElementCreationFilter
        {
            #region IUIElementCreationFilter Members

            void IUIElementCreationFilter.AfterCreateChildElements(UIElement parent)
            {
                // Do nothing
            }

            bool IUIElementCreationFilter.BeforeCreateChildElements(UIElement parent)
            {
                MaskedEditUIElement maskedEditUIElement = parent as MaskedEditUIElement;
                if (maskedEditUIElement != null)
                {
                    // When the control has focus (and is therefore in edit mode), do nothing.
                    UltraMaskedEdit ultraMaskedEdit = (UltraMaskedEdit)maskedEditUIElement.Control;
                    if (ultraMaskedEdit.ContainsFocus)
                        return false;

                    maskedEditUIElement.ChildElements.Clear();
                    TextUIElement textUIElement = new TextUIElement(parent, "MM/YYYY");
                    textUIElement.Rect = parent.RectInsideBorders;
                    maskedEditUIElement.ChildElements.Add(textUIElement);
                    return true;
                }

                // do nothing
                return false;
            }

            #endregion
        }

    I'm not accounting for localization here, so you might want to change the text string I used to account for the date separator character and you might want to adjust it based on the actual input mask being used instead of hard-coding the string, but this should point you in the right direction.