<tfoot id='oK38Z'></tfoot>

    <i id='oK38Z'><tr id='oK38Z'><dt id='oK38Z'><q id='oK38Z'><span id='oK38Z'><b id='oK38Z'><form id='oK38Z'><ins id='oK38Z'></ins><ul id='oK38Z'></ul><sub id='oK38Z'></sub></form><legend id='oK38Z'></legend><bdo id='oK38Z'><pre id='oK38Z'><center id='oK38Z'></center></pre></bdo></b><th id='oK38Z'></th></span></q></dt></tr></i><div id='oK38Z'><tfoot id='oK38Z'></tfoot><dl id='oK38Z'><fieldset id='oK38Z'></fieldset></dl></div>
  1. <small id='oK38Z'></small><noframes id='oK38Z'>

        <bdo id='oK38Z'></bdo><ul id='oK38Z'></ul>
    1. <legend id='oK38Z'><style id='oK38Z'><dir id='oK38Z'><q id='oK38Z'></q></dir></style></legend>

      通过代码选择多个Listbox项

      Selecting multiple Listbox items through code(通过代码选择多个Listbox项)
        <legend id='hLURY'><style id='hLURY'><dir id='hLURY'><q id='hLURY'></q></dir></style></legend>
        • <bdo id='hLURY'></bdo><ul id='hLURY'></ul>

                  <tbody id='hLURY'></tbody>
                <tfoot id='hLURY'></tfoot>

                <small id='hLURY'></small><noframes id='hLURY'>

                <i id='hLURY'><tr id='hLURY'><dt id='hLURY'><q id='hLURY'><span id='hLURY'><b id='hLURY'><form id='hLURY'><ins id='hLURY'></ins><ul id='hLURY'></ul><sub id='hLURY'></sub></form><legend id='hLURY'></legend><bdo id='hLURY'><pre id='hLURY'><center id='hLURY'></center></pre></bdo></b><th id='hLURY'></th></span></q></dt></tr></i><div id='hLURY'><tfoot id='hLURY'></tfoot><dl id='hLURY'><fieldset id='hLURY'></fieldset></dl></div>
              1. 本文介绍了通过代码选择多个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项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                Cannot remove items from ListBox(无法从列表框中删除项目)
                Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)

              2. <legend id='QlukQ'><style id='QlukQ'><dir id='QlukQ'><q id='QlukQ'></q></dir></style></legend>

                  <small id='QlukQ'></small><noframes id='QlukQ'>

                  <i id='QlukQ'><tr id='QlukQ'><dt id='QlukQ'><q id='QlukQ'><span id='QlukQ'><b id='QlukQ'><form id='QlukQ'><ins id='QlukQ'></ins><ul id='QlukQ'></ul><sub id='QlukQ'></sub></form><legend id='QlukQ'></legend><bdo id='QlukQ'><pre id='QlukQ'><center id='QlukQ'></center></pre></bdo></b><th id='QlukQ'></th></span></q></dt></tr></i><div id='QlukQ'><tfoot id='QlukQ'></tfoot><dl id='QlukQ'><fieldset id='QlukQ'></fieldset></dl></div>
                    • <bdo id='QlukQ'></bdo><ul id='QlukQ'></ul>

                          <tbody id='QlukQ'></tbody>
                      • <tfoot id='QlukQ'></tfoot>