5 Replies - 1844 Views - Last Post: 18 April 2014 - 07:16 PM Rate Topic: -----

#1 viper4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-April 14

Find and Select Multiple Items in ListBox

Posted 18 April 2014 - 04:27 PM

I have a listbox that retrieves a bunch of values.
I'm able to selectAll, Select None and Select Individual items. I'm not able to search the ListBox and select based on String value.

I have tried creating a for and if loop, but it will only select a single item (first item) in my ListBox.

        private void btnSelectCAD_Click(object sender, EventArgs e)
        {
            // Find CAD Line Patterns that Begin with IMPORT:
            string mySearchString = "IMPORT";
            // Search Listbox starting from index -1:
            int index = lstLinePatterns.FindString(mySearchString, -1);
            for (int i = 0; i < lstLinePatterns.Items.Count; i++)
            {
                if (index != -1)
                {
                    // Select the found items:
                    lstLinePatterns.SetSelected(index, true);
                }
                else
                    TaskDialog.Show("Error Message", "No CAD Line Patterns Found. ");
            }

        }


See Image.
Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: Find and Select Multiple Items in ListBox

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7143
  • View blog
  • Posts: 24,251
  • Joined: 05-May 12

Re: Find and Select Multiple Items in ListBox

Posted 18 April 2014 - 04:33 PM

Once you find one item's index, use that index value as the next starting point for the next possible match.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6823
  • View blog
  • Posts: 28,277
  • Joined: 12-December 12

Re: Find and Select Multiple Items in ListBox

Posted 18 April 2014 - 04:48 PM

The docs demonstrate this process:

ListBox.FindString Method (String, Int32) :MSDN
Was This Post Helpful? 1
  • +
  • -

#4 viper4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-April 14

Re: Find and Select Multiple Items in ListBox

Posted 18 April 2014 - 05:42 PM

I tried using the example from MSDN but the it doesn't ever get out of the loop.
I'm assuming the MSDN example is not wrong, so I don't know how to get out other than maybe changing
while(x != -1)
to something like
while (x != lstBoxTemp.Items.Count);


Revised Sample
        private void btnCAD_Click(object sender, EventArgs e)
        {
            // Find CAD Line Patterns that Begin with IMPORT:
            string searchString = "IMPORT";

           // Set our intial index variable to -1. 
           int x =-1;
           // If the search string is empty exit. 
           if (searchString.Length != 0)
           {
              // Loop through and find each item that matches the search string. 
              do
              {
                 // Retrieve the item based on the previous index found. Starts with -1 which searches start.
                 x = lstBoxTemp.FindString(searchString, x);
                 // If no item is found that matches exit. 
                 if (x != -1)
                 {
                    // Since the FindString loops infinitely, determine if we found first item again and exit. 
                    if (lstBoxTemp.SelectedIndices.Count > 0)
                    {
                       if(x == lstBoxTemp.SelectedIndices[0])
                          return;
                    }
                    // Select the item in the ListBox once it is found.
                    lstBoxTemp.SetSelected(x,true);
                 }

              }while(x != -1);
           }
        }

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6823
  • View blog
  • Posts: 28,277
  • Joined: 12-December 12

Re: Find and Select Multiple Items in ListBox

Posted 18 April 2014 - 06:08 PM

Make sure that SelectionMode.MultiExtended is set for the test-control that you have added, and that there is nothing currently initially selected.
Was This Post Helpful? 1
  • +
  • -

#6 viper4   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-April 14

Re: Find and Select Multiple Items in ListBox

Posted 18 April 2014 - 07:16 PM

I forgot to change the second test block to MultiExtended, thank you.

Final code.

        private void btnSelectCAD_Click(object sender, EventArgs e)
        {
            // Unselects any Items to Prevent Infinite Loop
            lstLinePatterns.SelectedIndex = -1;
            
            // Find CAD Line Patterns that Begin with IMPORT:
            string searchString = "IMPORT";

           // Set our intial index variable to -1. 
           int x =-1;
           // If the search string is empty exit. 
           //if (searchString.Length != 0)
           if (lstLinePatterns.SelectedIndices.Count ==0)
           {
              // Loop through and find each item that matches the search string. 
              do
              {
                 // Retrieve the item based on the previous index found. Starts with -1 which searches start.
                 x = lstLinePatterns.FindString(searchString, x);
                 // If no item is found that matches exit. 
                 if (x != -1)
                 {
                    // Since the FindString loops infinitely, determine if we found first item again and exit. 
                    if (lstLinePatterns.SelectedIndices.Count > 0)
                    {
                       if(x == lstLinePatterns.SelectedIndices[0])
                          return;
                    }
                    // Select the item in the ListBox once it is found.
                    lstLinePatterns.SetSelected(x,true);
                 }

              //} while (x != lstBoxTemp.Items.Count);
              } while (x != -1);

           }
            if (lstLinePatterns.SelectedIndices.Count < 1)
            {
                TaskDialog.Show("Warning Message", "No CAD Line Patterns Found. ");
            }
        }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1