Why can't I leave a TextBox using tab?

旧巷老猫 提交于 2019-12-24 10:46: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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/9272719/why-cant-i-leave-a-textbox-using-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!