Click here to Skip to main content
16,012,153 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi everyone,

I have this code:
C#
public static void AddDefaultTextFromTag(params TextBox[] textBoxes)
{
    foreach (TextBox oTextBox in textBoxes)
    {
        bool isPasswordChar = oTextBox.UseSystemPasswordChar;

        oTextBox.Enter += (sndr, evnt) =>
        {
            if (((TextBox)sndr).Text == ((TextBox)sndr).Tag.ToString())
            {
                ((TextBox)sndr).Text = "";
                ((TextBox)sndr).UseSystemPasswordChar = isPasswordChar;
                ((TextBox)sndr).ForeColor = SystemColors.WindowText;
            }
        };

        oTextBox.Leave += (sndr, evnt) =>
        {
            if (((TextBox)sndr).Text.Trim().Count() == 0)
            {
                ((TextBox)sndr).UseSystemPasswordChar = false;
                ((TextBox)sndr).CharacterCasing = CharacterCasing.Normal;
                ((TextBox)sndr).Text = ((TextBox)sndr).Tag.ToString();
                ((TextBox)sndr).ForeColor = SystemColors.GrayText;
            }
        };

        if (oTextBox.Text.Trim().Count() == 0)
        {
            oTextBox.UseSystemPasswordChar = false;
            oTextBox.CharacterCasing = CharacterCasing.Normal;
            oTextBox.Text = oTextBox.Tag.ToString();
            oTextBox.ForeColor = SystemColors.GrayText;
        }
    }
}


But when the TextBox.UseSystemPasswordChar I input in this method's parameter is true and it's TextBox.Text property is empty, the TextBox can't leave using a Tab button on the keyboard, only a MouseClick can be used to lose the focus of that TextBox.

Why is this happening and how can I fix this bug?

My code is in C#, framework 4, build in VS2010 Pro, project is in WinForms.
I use a TextBox from the VS ToolBox.

Please help. Thanks in advance.
Posted
Updated 13-Feb-12 21:48pm
v3
Comments
Sergey Alexandrovich Kryukov 14-Feb-12 2:36am    
Broken MSDN or something? :-)
--SA
Ignacioooooo 3-May-20 16:37pm    
Same thing happens to me! Have you found a solution? Thanks!

Behavior of this control on TAB is defined by its property System.Windows.Forms.TextBoxBase.AcceptsTab; please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.acceptstab.aspx[^].

—SA
 
Share this answer
 
Comments
unknowndentified10111 14-Feb-12 2:40am    
Gets or sets a value indicating whether pressing the TAB key in a multiline text box control types a TAB character in the control instead of moving the focus to the next control in the tab order. - but my TextBox is not MultiLine.
I think there is some problem with AutoPostback property make it false... and make it true when you needed...
 
Share this answer
 
Comments
unknowndentified10111 14-Feb-12 3:46am    
My code is in C#, framework 4, build in VS2010 Pro, project is in WinForms.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900