通过代码选择多个Listbox项

Selecting multiple Listbox items through code(通过代码选择多个Listbox项)
本文介绍了通过代码选择多个Listbox项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经搜索了一段时间,但似乎找不到解决问题的方法,我尝试了多种方法来通过代码选择列表框中的多个项目,但是没有一个有效,我得到的最好结果是在我的列表框中选择了 1 个项目.

Hi there I have searched for a while now and can't seem to find a solution to my problem, I have tried multiple methods to select multiple items in my listbox through code however none have worked, The best result I got was 1 selected item in my listbox.

基本上我想选择多个相同值的项目.

Basically I want to select multiple items of the same value.

下面是我的代码,抱歉,如果我看起来像新手,但我是编程新手,还在学习基本知识.

below is my code, sorry if I seem newbie but I am new to programming and still learning basic stuff.

 foreach (string p in listBox1.Items)
 {
           if (p == searchstring) 
           {
                 index = listBox1.Items.IndexOf(p);
                 listBox1.SetSelected(index,true);

           }
 }

如您所见,我试图告诉程序循环遍历列表框中的所有项目,并为每个等于searchstring"的项目获取索引并将其设置为选中.

So as you can see I am trying to tell the program to loop through all the items in my listbox, and for every item that equals "searchstring" get the index and set it as selected.

然而,这段代码所做的只是选择列表中等于searchstring"的第一个项目使其被选中并停止,它不会遍历所有searchstring"项目.

However all this code does is select the first item in the list that equals "searchstring" makes it selected and stops, it doesn't iterate through all the "searchstring" items.

推荐答案

按照评论中的建议,您应该将 SelectionMode 设置为 MulitSimpleMultiExpanded 取决于你的需要,但你也需要使用 forwhile 循环而不是 foreach,因为 foreach 循环不允许在迭代期间更改集合.因此,即使设置此属性也不会使您的代码运行,您将获得异常.试试这个:

As suggested in the comment, you should set SelectionMode to either MulitSimple or MultiExpanded depending on your needs, but you also need to use for or while loop instead offoreach, because foreach loop doesn't allow the collection to be changed during iterations. Therefore, even setting this Property won't make your code run and you will get the exception. Try this:

for(int i = 0; i<listBox1.Items.Count;i++)
{
     string p = listBox1.Items[i].ToString();
     if (p == searchstring)
     {
          listBox1.SetSelected(i, true);

     }
}

您可以在使用设计器时在属性"窗口中设置 SelectionMode,也可以在 Form 的构造函数中使用以下代码:

You can set SelectionMode either in the Properties window when using designer or in, for instance, constructor of your Form using this code:

listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;

这篇关于通过代码选择多个Listbox项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)