![](/img/trans.png)
[英]C# - How to check if the user's input is present in the ComboBox (used Data Bound Items)
[英]Check if user input are in the comboBox items
這是我檢查用戶是否在comboBox中輸入空的方法
if (string.IsNullOrEmpty(comboBox.Text))
{
MessageBox.Show("No Item is Selected");
}
如何檢查用戶輸入是否在comboBox項中? 例如,comboBox項是a,b,c。 當用戶在comboBox中輸入“ d”然后離開時,必須顯示一個messageBox。
您可以嘗試像George所說的那樣在您的ComboBox的Leave EventHandler中放置類似的內容,檢查該項目是否包含在ComboBox的Item集合中。
private void comboBox1_Leave(object sender, EventArgs e)
{
ComboBox cb = (ComboBox)sender;
if (! cb.Items.Contains(cb.Text))
{
MessageBox.Show("No Item is Selected");
}
}
嘗試這個:
int resultIndex = -1;
resultIndex = comboBox.FindExactString("d");
if(resultIndex == -1)
{
MessageBox.Show("No Item is Selected");
}
在這種情況下,@ Mark Hall提供的答案是正確的。 但是,如果要限制用戶不要使用組合框中的項目集合中未包含的項目,建議將DropDownStyle的屬性更改為DropDownList。
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.