我有一个蒙面的文本框,需要在它们上设置最小/最大长度。当满足这些条件时,按钮将变为启用状态。
我正在考虑处理 TextChanged 事件以确定输入文本的长度并设置按钮启用值。
有更好的方法吗?
btnOK.Enabled = txtDataEntry.Text.Length >= MinDataLength && txtDataEntry.Text.Length <= MaxDataLength;
我有一个蒙面的文本框,需要在它们上设置最小/最大长度。当满足这些条件时,按钮将变为启用状态。
我正在考虑处理 TextChanged 事件以确定输入文本的长度并设置按钮启用值。
有更好的方法吗?
btnOK.Enabled = txtDataEntry.Text.Length >= MinDataLength && txtDataEntry.Text.Length <= MaxDataLength;
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; };
// 在你的 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;
}