簡體   English   中英

MaskedTextBox的最小/最大長度

[英]MaskedTextBox Minimum/Maximum Lengths

我有一個蒙版文本框,需要在其上設置最小/最大長度。 當滿足這些條件時,將啟用一個按鈕。

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

有沒有更好的方法?

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

//在您的texbox評估活動中

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

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

哪種方法甚至比您建議的簡單?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM