简体   繁体   English

组合框C#的TextChanged事件

[英]TextChanged event for combobox C#

Basically the in the code below i am trying to loop through the array and if the current text in the combobox does not match anything in the array it will throw an error. 基本上在下面的代码中,我试图遍历数组,如果组合框中的当前文本与数组中的任何内容都不匹配,则会抛出错误。 However i cannot get it to fire on the event TextChanged event. 但是我无法在事件TextChanged事件上触发它。

Any help is greatly appreciated 任何帮助是极大的赞赏

string[] SRtier1 = { "Option1", "Option2", "Option3", "Option4", "Option5" };        

private void SRcmb_tier1_TextChanged(object sender, EventArgs e)
    {
        //Loop SRtier1 Array to ComboBox
        for (int i = 0; i < SRtier1.Length; i++)
        {
            if (SRcmb_tier1.Text != SRtier1[i])
            {
                MessageBox.Show("Please select one of the options provided.");
            }
        }
    }

如果您在TextChanged中有任何内容,请在事件中检查属性窗口

First, you have wrong loop implementation : in your code you're eager to fire the message SRtier1.Length times, what you want is, probably, to check the input and fire the message once: 首先,您有错误的循环实现 :在您的代码中您急于触发消息SRtier1.Length次,您想要的可能是检查输入并触发一次消息:

private void SRcmb_tier1_TextChanged(object sender, EventArgs e)
{
    Boolean found = false;

    for (int i = 0; i < SRtier1.Length; i++)
    {
        if (SRcmb_tier1.Text == SRtier1[i])
        {
            found = true;
            break;
        }
    }

    if (!found)
       MessageBox.Show("Please select one of the options provided.");
}

Better solution is to use Linq : 更好的解决方案是使用Linq

   private void SRcmb_tier1_TextChanged(object sender, EventArgs e) {
     if (!SRtier1.Any(item => item == SRcmb_tier1.Text)) 
       MessageBox.Show("Please select one of the options provided.");
   }

And finally, check if SRcmb_tier1_TextChanged is assinged to TextChanged of SRcmb_tier1 as von v. said in comments. 最后,检查是否SRcmb_tier1_TextChanged被assinged到TextChangedSRcmb_tier1冯诉的评论说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM