如何验证组合框 [英] how to validate a combobox
本文介绍了如何验证组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已将此方法用于必填字段组合框,但问题是.我想验证带有空格的组合框.我不想让用户在字符串的开头和结尾输入空格,也不想让用户一个接一个地输入多个空格,例如adil shaikh,我该如何实现呢?
i have used this method for required field combo box but the problem is. i want to validate combo box with spaces. i dont want user to enter spaces at the start and end of string and i dont want user to enter more than one whitespaces one after the other like adil shaikh how can i achive this?
public bool valcombo()
{
if (comboBox1.Text == string.Empty)
{
return true;
}
else
{
return false;
}
}
推荐答案
您可以尝试以下操作:
You can try this:
public bool valcombo()
{
//It will prevent space in begin, End and double space within the text
if (comboBox1.Text.StartsWith(" ")|| comboBox1.Text.EndsWith(" ")|| comboBox1.Text.Contains(" "))
{
return true;
}
else
{
return false;
}
}
您可以简单地使用它.
You can simply use this.
private Boolean valcombo(){
if ((comboBox1.Text.Length - comboBox1.Text.Trim().Length > 0) ||
(comboBox1.Text.Trim().Length - comboBox1.Text.Trim().Replace(" ", "").Length > 1))
return true;
else
return false;
}
input output
" adil shaikh" true
"adil shaikh " true
"adil shaikh" true
"adil shaikh" false
这篇关于如何验证组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文