简体   繁体   English

为什么我不能使用选项卡离开文本框?

[英]Why can't I leave a TextBox using tab?

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 .但是当我在这个方法的参数中输入的TextBox.UseSystemPasswordChar为 true 并且它的TextBox.Text属性为空时, TextBox不能使用键盘上的Tab按钮离开,只能使用MouseClick来失去该TextBox的焦点.

Why is this happening?为什么会这样?

My code is in C#, framework 4, build in VS2010 Pro, project is in WinForms.我的代码是 C#,框架 4,在 VS2010 Pro 中构建,项目在 WinForms 中。 I use a TextBox from the VS ToolBox.我使用 VS ToolBox 中的 TextBox。

Please help.请帮忙。 Thanks in advance.提前致谢。

The reason you can't leave the textbox is because you are changing the CharacterCasing property in the textbox.您不能离开文本框的原因是因为您正在更改文本框中的 CharacterCasing 属性。

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您还应该使用 new 关键字来“覆盖”(我知道这不是正确的术语)字符大小写,因此它不是自己的事情

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)代码基本上检查按下的键是否是一个字母,然后,如果它被标记为 Upper,并且字符较低,则将其替换为它的高版本(在光标位置),然后将光标移动到下一部分,并且反之亦然(到下)

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注意:如果用户选择了多个字符(SelectionLenght > 0),此代码可能(应该)有一些麻烦,如果您想保留正常的文本框功能,您应该删除所有选定的字符

What I found in the answer of this post was the solution for me.我在这篇文章的答案中找到了适合我的解决方案。 Instead of setting UseSystemPasswordChar to true and then to false, you can set PasswordChar to '●' and then to '\\0' to have normal text.您可以将 PasswordChar 设置为 '●' 然后设置为 '\\0' 以获得普通文本,而不是将 UseSystemPasswordChar 设置为 true 然后设置为 false。 You should not set the UseSystemPasswordChar because it has precedence over PasswordChar.您不应设置 UseSystemPasswordChar,因为它优先于 PasswordChar。

So I set up a WinForms app, drew two textboxes, set one to UseSystemPasswordChar=true then set it up like so:所以我设置了一个 WinForms 应用程序,绘制了两个文本框,将一个设置为 UseSystemPasswordChar=true 然后像这样设置:

    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 (添加了一个按钮,它对 Tab 测试没有任何作用)所以......除非我的测试设置无效,否则不会重现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM