如何比较两个组合项目,即使至少一个匹配都发现了C#

发布于 2025-02-11 07:38:33 字数 585 浏览 1 评论 0原文

我正在尝试比较两个组合项目,如果它们甚至具有单个项目匹配该项目,以及如何设置显示消息的条件作为匹配?

之类的ID

Combo10已经具有1001 1003 1004 1100

CombObox1获取Combo10 Text选择

我的问题时如何使用C#(Winforms)的重复项目(ID)来计算

下面的ComboBox1(Winforms)是我在按钮事件中使用的代码,但是

    var itms1 = combo10.Items;
    var itms2 = comboBox1.Items;

    foreach (var itm in itms2)
    {
        if (comboclass.Text == lblclasstext.Text)
        {
            if (itms1 == itm)
            {
                lbldone.Visible = true;
                lbltomark.Text = "";
            }
        }
    }

有人可以帮助吗?

I am trying to compare two comboboxes items if they have even single items match count that item and how to set condition for display message as match found?

combo10 already has IDs like

1001
1003
1004
1100

comboBox1 gets IDs when combo10 text get selected

My question is how to count repeated items(IDs) of combobox1 using c#(winforms)

Below is the code i have used in button event but it is not working

    var itms1 = combo10.Items;
    var itms2 = comboBox1.Items;

    foreach (var itm in itms2)
    {
        if (comboclass.Text == lblclasstext.Text)
        {
            if (itms1 == itm)
            {
                lbldone.Visible = true;
                lbltomark.Text = "";
            }
        }
    }

Can anybody help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

够钟 2025-02-18 07:38:33

其他替代方法是双循环以比较每个组合的列表

int repeatedItems = 0;
foreach (var cbItems1 in itms1)
{
    foreach (var cbItems2 in itms2)
     {
          if(cbItems1 == cbItems2){
              repeatedItems++;
         }  
     }
}

Other alternative method is to double loop to compare list of each comboboxes

int repeatedItems = 0;
foreach (var cbItems1 in itms1)
{
    foreach (var cbItems2 in itms2)
     {
          if(cbItems1 == cbItems2){
              repeatedItems++;
         }  
     }
}
亢潮 2025-02-18 07:38:33

将其适应您的代码,您应该得到所需的东西。

        Dictionary<string, string> d = new Dictionary<string, string>();
        //add item if it does not exists
        if (!d.ContainsKey("1001"))
        {
            d.Add("1001", "XPTO");
        }
        //get the itens in dictionary
        var x = d.Count;
        if (x > 0)
        {
            //do stuff...
        }

Adapt this to your code and you should get what you need.

        Dictionary<string, string> d = new Dictionary<string, string>();
        //add item if it does not exists
        if (!d.ContainsKey("1001"))
        {
            d.Add("1001", "XPTO");
        }
        //get the itens in dictionary
        var x = d.Count;
        if (x > 0)
        {
            //do stuff...
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文