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),則此代碼可能(應該)會有問題爲了保持正常的文本框功能,您應該刪除所有選定的字符

相關問題