2012-02-14 67 views
1

我有这样的代码:为什么我不能使用标签保留文本框?

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。

请帮忙。提前致谢。

+1

只是一个建议:为什么不创建两个方法(Enter和Leave)并将它作为事件处理程序附加到所有文本框,而不是使用每个自定义委托一? (我不认为它们在这种情况下被重用,但我不确定)而不是复制代码,您可以在所有这些代码上运行Leave事件处理程序。 – Joey 2012-02-14 06:40:21

+1

@Joey匿名方法是绝对重用的。在编译期间,每个匿名方法都转换为该类的实例方法,并且该方法用于事件处理程序。所以不要担心:) – 2012-02-14 07:07:17

+0

啊,谢谢MD.Unicorn。然后可以忽略该部分,并使用最后一个减少代码重复的建议;) – Joey 2012-02-14 12:54:50

回答

0

所以我成立了一个WinForms应用程序,画了两个文本框,设置一个UseSystemPasswordChar = true,那么设置它就像这样:

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

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

你的功能工作正常,我有没有问题,通过控制上的互联无论文本框包含什么形式。 (添加一个按钮也没有做任何事情,所以...没有repro,除非我的测试设置无效

+0

我从工具箱中拖动工具。 'TabIndex'由VS自动设置,'TabStop'设置为true。 – 2012-02-14 06:56:41

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

的代码基本上检查,如果按下的键是一个字母,那么,如果它被标记为上,并且char较低,用它的较高版本(在光标位置)替换它n将光标移动到下一部分,并且反之亦然(toLower)

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