0

I have the following class called "PlaceholderTextBox" which to act as a textbox with a placeholder text:

internal class PlaceholderTextBox : TextBox
{
    private bool isPlaceHolder = true;
    private bool isPassword;
    private string _PlaceholderText;
    private string _InputText;

    public string PlacehoderText
    {
        get { return _PlaceholderText; }
        set { _PlaceholderText = value; setPlaceholder(); }
    }

    public string InputText
    {
        get { return _InputText; }
        set { _InputText = value; }
    }

    public bool IsPassword
    {
        get { return isPassword; }
        set { isPassword = value; }
    }

    private void setPlaceholder()
    {
        if (string.IsNullOrEmpty(this.Text))
        {
            this.Text = PlacehoderText;
            InputText = "";
            this.ForeColor = System.Drawing.Color.Gray;
            isPlaceHolder = true;
            if (isPassword) this.UseSystemPasswordChar = false;
        }
    }

    private void removePlaceHolder()
    {
        if (isPlaceHolder)
        {
            this.Text = "";
            this.ForeColor = System.Drawing.SystemColors.WindowText;
            isPlaceHolder = false;
            if (isPassword) this.UseSystemPasswordChar = true;
        }
    }

    public void resetText()
    {
        isPlaceHolder = true;
        this.Text = null;
        setPlaceholder();
    }

    public PlaceholderTextBox()
    {
        Enter += removePlaceHolder;
        Leave += setPlaceholder;
        TextChanged += addText;
    }

    private void setPlaceholder(object sender, EventArgs e)
    {
        setPlaceholder();
    }

    private void removePlaceHolder(object sender, EventArgs e)
    {
        removePlaceHolder();
    }

    private void addText(object sender, EventArgs e)
    {
        InputText = this.Text;
    }
}

In my main Form, I have two of these PlaceholderTextBoxes, one for "Username" and the other for "Password", which also has bullets cover for protecting the text entered. The problem I found is that, if I want to switch from the password textbox to the other one using "Tab" key, it doesn't work. It works fine if it has some text or if I want to switch from Username to Password, but if I don't have any text in the Password box and I want to switch to Username, it doesn't work.

I have absolutely no idea why is not working, my guess would be it has something to do with the password cover system. Any insights on this issue?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Sima Tudor
  • 13
  • 4
  • 2
    Have you tried setting the [TabIndex](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.tabindex(v=vs.110).aspx) of any forms? – Matt Clark May 16 '17 at 20:30
  • 1
    You must also have [TabStop](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.tabstop(v=vs.110).aspx) set on each element. – Matt Clark May 16 '17 at 20:31
  • Instead of doing your very own _cue banner_ (this is what you call "placeholder"), why not [use the native Windows way of doing it](http://stackoverflow.com/a/4902969/107625)? It uses the [`EM_SETCUEBANNER` message](https://msdn.microsoft.com/en-us/library/windows/desktop/bb761639.aspx). – Uwe Keim May 16 '17 at 20:32
  • The Username textbox has tabindex = 1, the password has tabindex = 2. This works fine as long as I don't tab when I am on password textbox with no text entered. – Sima Tudor May 16 '17 at 20:33
  • 1
    The CueTextBox is working like a charm. Thanks a lot Uwe Keim! – Sima Tudor May 16 '17 at 20:36
  • Do you perhaps have turned on `AcceptsTab` ? – TaW May 16 '17 at 20:42

0 Answers0