listbox1中的项目数是以(“A1”)和(B1)开头的? C# [英] Count of items in the listbox1 what starting with ("A1") and (B1)? C#

查看:55
本文介绍了listbox1中的项目数是以(“A1”)和(B1)开头的? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



listBox1中总计数项的代码,知道每个:



Hi,
Code for total count items in listBox1, knows each:

label1.Text = lisBox1.Items.Count.ToString();





我需要,listBox1中的项目数以<$ c开头$ c>(A1)和(B1)。谢谢。?



我的尝试:



之类的东西(我知道这不正确):





I need, count of items in the listBox1 what starting with ("A1") and ("B1"). Thanks.?

What I have tried:

something like (I know that this is not correct):

label1.Text = lisBox1.Items.Count.StartWith("A1").ToString();
label1.Text = lisBox1.Items.Count.StartWith("B1").ToString();

推荐答案

如果列表框中的所有项目都是字符串,那么试试这个:

If all the items in the listbox are strings then try this:
int numberOfA1 = myListBox.Items.Cast<string>().Where(s => s.StartsWith("A1")).Count();
int numberOfB1 = myListBox.Items.Cast<string>().Where(s => s.StartsWith("B1")).Count();


以下是C#中编辑过的代码


Here is the edited code in C#

int cases = 0;
           for (int i = 0; i <= this.listBox1.Items.Count - 1; i++)
           {
               if (listBox1.Items[i].ToString().ToLower().StartsWith("A1".ToLower()))
               {
                   cases += 1;
               }

           }
           MessageBox.Show(cases.ToString());


这篇关于listbox1中的项目数是以(“A1”)和(B1)开头的? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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