2011-02-02 146 views
0

我有一個蒙面文本框,需要設置最小/最大長度。當這些條件滿足時,按鈕變爲啓用狀態。MaskedTextBox最小/最大長度

我正在考慮處理TextChanged事件以確定輸入文本的長度並設置按鈕啓用值。

有沒有更好的方法?

btnOK.Enabled = txtDataEntry.Text.Length >= MinDataLength && txtDataEntry.Text.Length <= MaxDataLength; 

回答

0

哪種方法能比你的建議是什麼就更簡單了?

myTextBox.Textchanged+=(s,o)=>{ myButton.Enabled = myTextBox.Length==10; }; 
0

IMO TextChanged事件是處理此功能條件的好地方。

更新

做,在這樣的KeyPress事件:

maskedtxtbox.KeyPress => (s , ev) { 
        if(maskedtxtbox.Length > 9) 
        { 
         //This prevent from key to go to control 
         e.Handled =true; 
         button1.Enabled = true; 
        } 
       }; 
+0

查看更新後的問題。我如何防止他們輸入超過最大長度? – Jon 2011-02-02 15:41:04

+0

我已更新我的回答 – 2011-02-02 15:46:34

0

//在你texbox valdating事件

private void textBox4_Validating(object sender, CancelEventArgs e) 
    { 
     TextBox tb = sender as TextBox; 
     if (tb != null) 
     { 
      int i=tb.Text.Length; 
      //Set your desired minimumlength here '7' 
      if (i<7) 
      { 

       MessageBox.Show("Too short Password"); 
       return; 

      } 
     } 
     else 

     e.Cancel = true; 
    }