Link to home
Start Free TrialLog in
Avatar of lms036000
lms036000

asked on

ListBox, selectionMode: multiple , selected item does not scroll to the top

I have listbox , and its selectionmode is multiple. when I select an item programmatically, the item gets selected(gets highlighted in the listbox), however the selected item does not get scroll to the top. I am using web C#.
Avatar of alexmac05
alexmac05
Flag of United States of America image

I believe that you will have to write an algorithm to add this functionality.

I wrote this to show how to just swap the two values but you would need something that would change them such that all of the values would switch.

int intIndex = listBox1.SelectedIndex;

if (intIndex != -1) //when something is unselected it is -1
{
   string strSelectedItem = listBox1.Items[intIndex].ToString();
   string strZeroItem = listBox1.Items[0].ToString();

   listBox1.Items[0] = strSelectedItem;
   listBox1.Items[intIndex] = strZeroItem;
}



The fact that you have multiselect turned on does also complicate it.

I do not know of another way to do this other than to write the code yourself.
ASKER CERTIFIED SOLUTION
Avatar of zonkerman
zonkerman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
okay one minor problem with that sample I provided.  I used 92 as the sample item number to select but it will only scroll to the top if the listbox on your form is sized to display 8 items at once.  I therefore forgot to say make sure for the sample to make a listbox that displays only 8 items.  The routine SelectItemAndScrollToTop should work regardless of the size of the listbox provided it is possible to scroll the selected item to the top.