none
Programatically select items in ListBox

    Question

  • I know this is a complete newbee question, this is becuase I am new to VB, how would I go about programatically selecting items in a litsbox?

    I need a quick way for the user to be able to select all items in the list.

    Thanks in advance

    Monday, May 09, 2005 10:53 PM

Answers

All replies

  • Hi,

    here is a link with sample code for listbox
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformslistboxclassitemstopic.asp


    you can cycle through the items of the listbox with a for-loop

    for(int i = 0; i < listbox.items.count;i++)

    and then select them

    listbox.setSelected(i,true);

    hope this helps.
    Tuesday, May 10, 2005 8:28 AM
    Moderator
  • Thanks Ralph. I knew it had to be easy like that.
    Tuesday, May 10, 2005 2:55 PM
  • Ah but Ralph gave you C# syntax on the VB newsgroup...  Here's some VB code:

    for i as integer = 0 to listbox1.items.count -1
       listbox1.setselected(i,True)
    next

    You also need to make sure your selection mode is one of the multi-select options, ie:

    ListBox1.SelectionMode = SelectionMode.MultiExtended

    Adam

    Wednesday, May 11, 2005 9:02 PM
    Moderator
  • Thanks Adam.

    I realized after I copied that code into my project that it was C#. Luckily I have been working with C++ for a while so I was able to convert it.

    Friday, May 13, 2005 3:08 PM
  • To select all items and keep selected item focused and visible try this:

    void SelectAll()
     {
                //do whole operation within one update
                lb.BeginUpdate();
               
                int topIndex = lb.IndexFromPoint(0, 0);
                int selectedIndex = lb.SelectedIndex;

                for (int i = 0; i < lb.Items.Count; i++) lb.SelectedIndex = i;
               
                // scroll back to first visible item
                lb.TopIndex = topIndex;
               
                // to return focus to selected item it must be unselected and selected again
                lb.SetSelected(selectedIndex, false);
                lb.SetSelected(selectedIndex, true);
               
                lb.EndUpdate();
    }
    Monday, October 29, 2007 9:06 PM
  • To select all items and keep selected item focused and visible try this:

    void SelectAll()
     {
                //do whole operation within one update
                lb.BeginUpdate();
               
                int topIndex = lb.IndexFromPoint(0, 0);
                int selectedIndex = lb.SelectedIndex;

                for (int i = 0; i < lb.Items.Count; i++) lb.SelectedIndex = i;
               
                // scroll back to first visible item
                lb.TopIndex = topIndex;
               
                // to return focus to selected item it must be unselected and selected again
                lb.SetSelected(selectedIndex, false);
                lb.SetSelected(selectedIndex, true);
               
                lb.EndUpdate();
    }

    this is the best answer!!!!
    Monday, October 03, 2011 3:27 PM