Implementing a PasswordChar property to my custom C# Windows Form TextBox class that supports placeholders

StackOverflow https://stackoverflow.com/questions/20901645

Question

I'm trying to implement a PasswordChar property to my TextBox class that cooperates with my Placeholder class. This is my code:

class MyTextBox : TextBox
{
    public Placeholder Placeholder;

    public MyTextBox()
    {
        Placeholder = new Placeholder(this);

        GotFocus += new EventHandler(_GotFocus);
        LostFocus += new EventHandler(_LostFocus);
    }

    private void _GotFocus(object sender, EventArgs e)
    {
        Placeholder.Active = false;
    }

    private void _LostFocus(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Text))
            Placeholder.Active = true;
    }
}

class Placeholder
{
    private MyTextBox textBox;
    private bool active = false;
    public bool Active
    {
        get { return active; }
        set
        {
            if (value)
            {
                textBox.ForeColor = ForeColor;
                textBox.Text = Text;
            }
            else if (active && !value)
            {
                textBox.ForeColor = previousForeColor;
                textBox.Text = string.Empty;
            }

            active = value;
        }
    }
    private Color previousForeColor;
    private Color foreColor = Color.Gray;
    public Color ForeColor
    {
        get { return foreColor; }
        set
        {
            previousForeColor = ForeColor;
            ForeColor = value;
        }
    }
    private string text = "Placeholder";
    public string Text
    {
        get { return text; }
        set
        {
            text = value;

            if (active)
                textBox.Text = Text;
        }
    }

    public Placeholder(MyTextBox textBox)
    {
        this.textBox = textBox;
    }
}

The way I want it to work is if I set the TextBox's property PasswordChar to anything but '\0' then it should be set to '\0' when the TextBox's property's Placeholder's property Active is true, and should be set back to whatever the PasswordChar was when it's set to false. In other words, if PasswordChar is set then unmask the text when the Active property is true, and remask the text when it's set back to false.

Was it helpful?

Solution

It sounds as simple as storing the original PasswordChar in a temporary variable:

class Placeholder
{
    private MyTextBox textBox;
    private bool active = false;
    private char? originalPasswordChar = null;
    public bool Active
    {
        get { return active; }
        set
        {
            if (value)
            {
                textBox.ForeColor = ForeColor;
                textBox.Text = Text;
                if (originalPasswordChar == null) originalPasswordChar = textBox.PasswordChar;
            }
            else if (active && !value)
            {
                textBox.ForeColor = previousForeColor;
                textBox.Text = string.Empty;
                textBox.PasswordChar = originalPasswordChar.Value;
                originalPasswordChar = null; 
            }

            active = value;
        }
    }
    // (...)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top