Tasnim Fabiha
Tasnim Fabiha

Reputation: 1238

Hiding the text of a TextBox control without using a PasswordBox

I am using a TextBox control for the user input in my Windows Phone 8.1 app.
How can I hide the characters as user gives input?

I am not using a PasswordBox because the defined InputScope is "Number" which is not possible in a PasswordBox.

While searching for a solution on the internet I found the only way by customizing the TextBox with the help of an UserControl.

Is there any easier way to do this without creating any UserControl?
Following is my code snippet:

In 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 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 = "";
    }
}

Upvotes: 6

Views: 5091

Answers (2)

Tasnim Fabiha
Tasnim Fabiha

Reputation: 1238

After spending hours in finding an easier way I got an amazing solution. Hope this would help others too.
I simply added the following value to the FontFamily property of my TextBox control:

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

And gave the size of font 35,

FontSize="35"

This works just fine for my project.

Upvotes: 2

rmojab63
rmojab63

Reputation: 3631

I managed to create a custom TextBox, in which Text is *, but there is hiddenText that keeps the real string. Note that managing Caret position is not easy, because it changes due to some internal logic. Therefore, it is always at the end of the string. (Also note that you might need to handle some exceptions and bugs)

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;
    }

}

Upvotes: 0

Related Questions