0

I'm am trying to create a custom TextBox that display an empty string when it is disabled (Enabled = false). When I enable it again, it should display the last value. Also, the Text property should always return the real value, even if it's disabled.

Here is what I have so far, but it's not really working as I want.

Can you help me figure out what is wrong?

public class TestTextBox: System.Windows.Forms.TextBox
{
    private string _text = string.Empty;
    public new string Text
    {
        get
        {
            return _text;
        }
        set
        {
            _text = value;
            base.Text = FormatValue(value);
        }
    }

    protected override void OnEnabledChanged(EventArgs e)
    {
        base.OnEnabledChanged(e);

        base.Text = FormatValue(_text);
    }

    private string FormatValue(string text)
    {
        if(this.Enabled == true)
        {
            return text;
        }
        else
        {
            return string.Empty;
        }
    }
}
2
  • 3
    What is it doing wrong? Commented Sep 11, 2014 at 17:22
  • It won't go back to original text after I enable it. Commented Sep 11, 2014 at 17:26

2 Answers 2

2

I think the mistake in the code is that typing in it doesn't change its Text property.

If you add a button with:

testTextBox1.Text = "It works";

then enabling and disabling the TestTextBox will toggle between empty and "It works".

EDIT

Add:

protected override void OnTextChanged(EventArgs e)
{
    base.OnTextChanged(e);
    if (Enabled)
        Text = base.Text;
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is a comment, not an answer!
@Pierre-OlivierGoulet I tested it. It works. (By the way, +1 on the question. non-trivial mistake there.)
Yeah, it works as long as I don't type in the textbox... How can I make it work also when typing?
2

How about something like this:

protected override void OnEnabledChanged(EventArgs e)
{
    base.OnEnabledChanged(e);

    this.ForeColor = this.Enabled ? SystemColors.ControlText : this.BackColor;
}

If you want to support other ForeColors you can store the current one in a Property and use it instead of SystemColors.ControlText.

4 Comments

This is by far the simplest solution. You can also use Color.Transparent instead of this.BackColor
Yes, I have come to stay away from Transparency, but it ought to work here ;-)
While this wokrs, I'll keep it as a last resort because it is not 'politicaly correct'...
Well I thought so myself and in fact a spy tool would be able to read the text. But if that's the concern I guess that solution will be the least of your concerns. Being disabled the text can't be selected either, which would make it visible, so i guess it is safe enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.