比较C#中列表中的项目 [英] compare the items in list in c#

查看:59
本文介绍了比较C#中列表中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将组合框的选定项目与其他组合框的选定项目进行比较 为此,我已经在列表中注册了所有组合框,并将其命名为"panel1kilist" 现在我面临的问题是,当两个组合框中有相同的项目时,消息框首先显示找不到mattch",然后显示找到匹配项",实际上它先转到内循环的else语句,然后转到if语句,帮助

i want to compare the selecteditem of combobox with the selecteditem of other combobox for this i have registered all the comboboxes in a list and named it "panel1kilist" now the problem i am facing is that when there are same items in two comboboxes first the messagebox shows "no mattch found" and then it shows "match found" actually it goes to the else statement of inner loop first and then to if statement kindly help

    private void button1_Click(object sender, EventArgs e)
        {
            bool check = false;
            bool check1 = false;[![in this image you can see that there are two same items but message is showing "no match found"][1]][1]
            try[![after clicking on ok button of message box showing "no match found" this message box shows up][1]][1]
            {
                for (int i = 1; i < panel1kilist.Count; i++)
                {
                    for (int j = i + 1; j < panel1kilist.Count; j++)
                    {
                        if (panel1kilist[i].SelectedItem.ToString() == panel1kilist[j].SelectedItem.ToString())
                        {
                            if (check == false)
                            {
                                MessageBox.Show("match found");
                            }
                            check = true;
                        }

                        else
                        {
                            if (check1 == false)
                            {
                                MessageBox.Show("no match found");
                            }
                            check1 = true;
                        }
                    }
                }
            }
            catch (System.NullReferenceException)
            {
                MessageBox.Show("please fill all the boxes first");
            }


        }

推荐答案

您的问题并不十分清楚,但我仍会尝试为您提供帮助.如注释中所述,您的代码中存在几个小问题:

Your question is not really clear but I still try to give you some help. As mentioned in the comments there are several small issues in your code:

1.外部循环

for (int i = 1; i < panel1kilist.Count; i++)

您定义i = 1,这意味着您跳过了panel1kilist中的第一项,因为列表和数组都从索引0开始

You define i = 1, which means you skip the very first item in your panel1kilist because lists and arrays start at index 0

2.使用布尔变量

if (panel1kilist[i].SelectedItem.ToString() == panel1kilist[j].SelectedItem.ToString())
{
     if (check == false)
     {
          MessageBox.Show("match found");
     }
     check = true;
}
else
{
     if (check1 == false)
     {
          MessageBox.Show("no match found");
     }
     check1 = true;
}

在开始for循环之前,请定义bool变量.因此,无论何时将变量check和check1设置为true,if条件check == false和check1 == false都将不再返回true.因此,除了第一个找到匹配项"和找不到匹配项"之外,您将永远不会收到任何消息.

You define your bool variables before starting your for-loops. So whenever you set your variables check and check1 to true, the if conditions check == false and check1 == false will never return true anymore. So you will never get any message except the very first "Match found" and "no match found".

3.我建议的解决方案

使用foreach循环并在找到匹配后中断循环,循环仅显示消息之后.代码:

Make use of the foreach-loop and break the loops once you found a match, after the loops just show the message. Code:

var matchFound = false;

foreach (var combobox in panel1kilist)
{
     foreach (var comboboxToMatch in panel1kilist.Skip(1))
     {
          if (combobox.SelectedItem.ToString() == comboboxToMatch.SelectedItem.ToString())
          {
               matchFound = true;
               // Stops the inner loop in case of a match
               break;
          }
     }

     // Stops the outer loop in case of a match
     if(matchFound)
     {
          break;
     }
}

if(matchFound)
{
     MessageBox.Show("match found");
}

这篇关于比较C#中列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆