Hide text in a TextBox without using a PasswordBox

I am using a TextBoxuser input control in my Windows Phone 8.1 application.
How to hide characters when user types ?

I do not use PasswordBoxbecause some InputScopethere "Number", which is impossible in PasswordBox.

When searching for a solution on the Internet, I found the only way I configured TextBoxwith UserControl.

Is there an easier way to do this without creating UserControl?
The following is a snippet of code:

On the XAML page:

<TextBox Text="{Binding CardNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    MaxLength="17"
    x:Name="CardNoTextBox"
    InputScope="Number"
    Margin="70,5"
    PlaceholderText="Enter Your Card Number"
    TextChanged="CardNoTextBox_TextChanged"
    BorderBrush="Gray"
    BorderThickness="2"
    FontSize="20"/>

In the code behind (xaml.cs):

private void CardNoTextBox_TextChanged(object sender, RoutedEventArgs routedEventArgs)
{
    if (IsTextAllowed(CardNoTextBox.Text))
    {
        if (CardNoTextBox.Text.Length == 5)
        {
            if (CardNoTextBox.Text[4] != ' ')
            {
                string text = CardNoTextBox.Text.Insert(4, " ");
                CardNoTextBox.Text = text;
                CardNoTextBox.Select(CardNoTextBox.Text.Length, 0);
            }
        }

        if (CardNoTextBox.Text.Length == 12)
        {
            if (CardNoTextBox.Text[11] != ' ')
            {
                string text = CardNoTextBox.Text.Insert(11, " ");
                CardNoTextBox.Text = text;
                CardNoTextBox.Select(CardNoTextBox.Text.Length, 0);
            }
        }
    }
    else
    {
        CardNoTextBox.Text = "";
    }
}
+4
2

, . , . FontFamily TextBox:

FontFamily="ms-appx:///Assets/PassDot.ttf#PassDot"

35,

FontSize="35"

.

0

TextBox, Text is *, hiddenText, . , Caret , - . . ( , )

public class HiddenTextBox : TextBox
{ 
    internal string hiddenText { get; private set; }

    protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
        if (e.Key == Key.Space)
            addText(" ");
        else if (e.Key == Key.Back)
            removeText(true);
        else if (e.Key == Key.Delete)
            removeText(false);
        else if (e.Key == Key.Return)
            e.Handled = true;
        base.OnPreviewKeyDown(e);
    }
    protected override void OnPreviewTextInput(TextCompositionEventArgs e)
    {
        addText(e.Text);
        e.Handled = true;
    }
    void addText(string text)
    {
        hiddenText = hiddenText != null ? hiddenText.Insert(CaretIndex, text) : text;
        update();
    }
    void removeText(bool back)
    {
        if (hiddenText == null || hiddenText.Length == 0 || (back==false && CaretIndex == hiddenText.Length))
            return;
        if (back)
            hiddenText = hiddenText.Substring(0, CaretIndex - 1) + hiddenText.Substring(CaretIndex, hiddenText.Length - CaretIndex);
        else
            hiddenText = hiddenText.Substring(0, CaretIndex) + hiddenText.Substring(CaretIndex+1, hiddenText.Length - CaretIndex);
        update();
    }
    void update()
    {
        StringBuilder star = new StringBuilder();
        foreach (var s in hiddenText)
        {
            star.Append("*");
        }
        Text = star.ToString();
    }

    protected override void OnTextChanged(TextChangedEventArgs e)
    {
        if (hiddenText != null)
            CaretIndex += hiddenText.Length;
    }

}
0

Source: https://habr.com/ru/post/1670300/


All Articles