Why can't I leave a TextBox using tab?

2019-08-15 05:35发布

I have this code:

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?

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.

2条回答
Summer. ? 凉城
2楼-- · 2019-08-15 05:59

The reason you can't leave the textbox is because you are changing the CharacterCasing property in the textbox.

Not sure why it works like this, but it has happened to me before, what I ended up doing was capture the keypress event, and if it was a letter I'd switch it to it's uppercase value. It's not optimal, but it works

I did something similar to this (writing it from the top of my head, but it should work):

void YourTextbox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (char.IsLetter(e.KeyChar))
    {
        if (this.CharacterCasing == CharacterCasing.Upper && char.IsLower(e.KeyChar))
        {
            this.Text = this.Text.Insert(this.SelectionStart, char.ToUpper(e.KeyChar) + string.Empty);
            this.SelectionStart++;
            e.Handled = true;
        }
        else if (this.CharacterCasing == System.Windows.Forms.CharacterCasing.Lower && char.IsUpper(e.KeyChar))
        {
            this.Text = this.Text.Insert(this.SelectionStart, char.ToLower(e.KeyChar) + string.Empty);
            this.SelectionStart++;
            e.Handled = true;
        }
    }
}

You also should use the new keyword to "override" (I know that's not the right term here) the Character casing, so it doesn't do it's own thing

public new CharacterCasing CharacterCasing { get; set; }

The code basically checks if the pressed key is a letter, then, if it's marked as Upper, and the char is lower, replaces it with it's upper version (in the position of the cursor) then moves the cursor to the next part, and Viceversa (toLower)

NOTE: This code will have may (should) have some trouble if the user has more than one character selected (SelectionLenght > 0), if you want to keep the normal Textbox functionality, you should delete all the selected characters

查看更多
萌系小妹纸
3楼-- · 2019-08-15 06:11

So I set up a WinForms app, drew two textboxes, set one to UseSystemPasswordChar=true then set it up like so:

    private void Form1_Load(object sender, EventArgs e)
    {
        textBox2.Tag = "test2";
        textBox1.Tag = "test1";

        TextBox[] tb = { textBox1, textBox2 };
        AddDefaultTextFromTag(tb);
    }

Your function works fine and I have no problems tabbing through the controls on the form no matter what the textboxes contain. (added a button also that does nothing for tabbing test) so... no repro unless my test setup is not valid

查看更多
登录 后发表回答