Skip to content

TextBoxMask TextChanging event handler bug #3279

Closed
@oenarap

Description

@oenarap

TextBoxMask leaves an artifact (i.e., old Text value) when a new value is set. (in my case, thru data binding)

since this is just a subtle bug, please allow me to not comply with your required details for this report.

and let me get you directly where i found the offending part of the code:

public partial class TextBoxMask
{
    ...

   private static void Textbox_TextChanging(TextBox textbox, TextBoxTextChangingEventArgs args) 
   {
   ...

    // case adding data at the end of the textbox
    if (oldSelectionStart >= oldText.Length && !isDeleteOrBackspace)
    {
        textbox.Text = oldText;
        if (oldText.Length >= 0)
        {
            textbox.SelectionStart = oldText.Length;
        }
            return;
        }
    ...
    }
}

1. within the if condition, textbox.Text will revert to old value even if you actually intended to assign a completely new/different value to it.

in the subsequent part of the same event handler:

        // Case change due to Text property is assigned a value (Ex Textbox.Text="value")
        if (textbox.SelectionStart == 0 && textbox.FocusState == FocusState.Unfocused)
        {
            var displayText = textbox.GetValue(DefaultDisplayTextProperty) as string ?? string.Empty;
           
            if (string.IsNullOrEmpty(textbox.Text))
            {
                textbox.Text = displayText;
            }
            else
            {
                ...
            }
        }
        ...
        textbox.Text = new string(textArray);
        ...
    }

2. the nested if condition will allow execution of the remaining part of the code which ends up in textbox.Text getting assigned with the old value, even if you actually intend to assign a null/emptly string to it.

here's the quick fix i came up with:

        ...
        // case adding data at the end of the textbox
        if (oldSelectionStart >= oldText.Length && !isDeleteOrBackspace)
        {
            // ignore change(s) if oldtext is a substring of new text value
            if (textbox.Text.Contains(oldText, StringComparison.OrdinalIgnoreCase))
            {
                textbox.Text = oldText;

                if (oldText.Length >= 0)
                {
                    textbox.SelectionStart = oldText.Length;
                }
                
                return;
            }
        }
        ...

        // Case change due to Text property is assigned a value (Ex Textbox.Text="value")
        if (textbox.SelectionStart == 0 && textbox.FocusState == FocusState.Unfocused)
        {
            string displayText = textbox.GetValue(DefaultDisplayTextProperty) as string ?? string.Empty;
            
            if (string.IsNullOrEmpty(textbox.Text))
            {
                textbox.SetValue(OldTextProperty, displayText);
                textbox.SetValue(OldSelectionStartProperty, 0);
                textbox.SetValue(OldSelectionLengthProperty, 0);
                textbox.Text = displayText;
                return;
            }
            else
            {
                ...
            }
        }
        ...

THANKS!

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PR 🚀bug 🐛An unexpected issue that highlights incorrect behaviorextensions ⚡good first issueIssues identified as good for first-time contributorshelp wantedIssues identified as good community contribution opportunities

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions