为什么我不能使用tab留下TextBox?

时间:2012-02-14 06:35:54

标签: c# winforms .net-4.0

我有这段代码:

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;
        }
    }
}

但是当我在此方法的参数中输入的TextBox.UseSystemPasswordChar为真并且TextBox.Text属性为空时,TextBox无法使用Tab按钮离开键盘,只有MouseClick可用于失去TextBox的焦点。

为什么会这样?

我的代码在C#,框架4中,在VS2010 Pro中构建,项目在WinForms中。 我使用VS ToolBox中的TextBox。

请帮忙。提前谢谢。

3 个答案:

答案 0 :(得分:0)

所以我设置了一个WinForms应用程序,绘制了两个文本框,将一个设置为UseSystemPasswordChar = true,然后将其设置为:

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

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

您的功能正常工作,无论文本框包含什么内容,我都可以通过表单上的控件进行标记。 (添加了一个按钮,对于标签测试也没有任何作用)所以...除非我的测试设置无效,否则没有重复

答案 1 :(得分:0)

您无法离开文本框的原因是您正在更改文本框中的CharacterCasing属性。

不确定为什么它会这样,但它之前发生在我身上,我最终做的是捕获按键事件,如果它是一个字母,我会将它切换为它的大写值。它不是最佳的,但它可以工作

我做了类似的事情(从头顶写下来,但它应该有效):

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;
        }
    }
}

您还应该使用new关键字来“覆盖”(我知道这里不是正确的术语)字符大小写,所以它不会做自己的事情

public new CharacterCasing CharacterCasing { get; set; }

代码基本上检查按下的键是否为字母,然后,如果它被标记为Upper,并且char更低,则将其替换为它的高级版本(在光标的位置),然后将光标移动到下一个部分和Viceversa(toLower)

注意: 如果用户选择了多个字符(SelectionLenght> 0),此代码可能(应该)有一些问题,如果要保留正常的文本框功能,则应删除所有选定的字符

答案 2 :(得分:0)

我在此post的答案中找到的是适合我的解决方案。您可以将PasswordChar设置为“●”,然后设置为“ \ 0”,以使用普通文本,而不是将UseSystemPasswordChar设置为true,然后设置为false。您不应设置UseSystemPasswordChar,因为它优先于PasswordChar。