简体   繁体   English

MaskedTextBox的最小/最大长度

[英]MaskedTextBox Minimum/Maximum Lengths

I have a masked textbox with the need to have a min/max length set on them. 我有一个蒙版文本框,需要在其上设置最小/最大长度。 When these conditions are met a button becomes enabled. 当满足这些条件时,将启用一个按钮。

I was thinking of handling the TextChanged event to determine the length of the entered text and set the buttons enabled value. 我正在考虑处理TextChanged事件,以确定输入的文本的长度并设置按钮的启用值。

Is there a better approach? 有没有更好的方法?

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

// At your texbox valdating Event //在您的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 event is good place to handle this feature condition. IMO TextChanged事件是处理此功能状况的好地方。

Update 更新资料

Do it in KeyPress event like this: 在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