Solved

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

Posted on 2008-10-09
6
2,196 Views
Last Modified: 2013-12-17
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#.
0
Comment
Question by:lms036000
  • 2
  • 2
6 Comments
 

Expert Comment

by:alexmac05
ID: 22683040
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;
}



0
 

Expert Comment

by:alexmac05
ID: 22683047
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.
0
 
LVL 1

Accepted Solution

by:
zonkerman earned 500 total points
ID: 22697315
This code I put together may do the trick.  First it populates a list box to 100 entries. Then it calls a routine I wrote which selects item 92 and scrolls it to the top of the list if it can be done.  

 
      private void button3_Click(object sender, EventArgs e)
        {
            PopulateListBox();
            SelectItemAndScrollToTop(listBox1, 92);
        }
        private void PopulateListBox()
        {
            for (int i = 0; i < 100; i++)
            {
                listBox1.Items.Add("entry " + System.Convert.ToString(i));
            }

        }

        private void SelectItemAndScrollToTop(System.Windows.Forms.ListBox aListBox, int SelectedIndex)
        {
            int LinesDisplayed = aListBox.ClientSize.Height / aListBox.ItemHeight;
            listBox1.SetSelected(92, true);
            if (
                (aListBox.Items.Count > LinesDisplayed) &&
                (
                  (aListBox.Items.Count - aListBox.SelectedIndex) > (LinesDisplayed * 2) ||
                  (  
                    (aListBox.SelectedIndex > LinesDisplayed) &&
                    ((aListBox.Items.Count - aListBox.SelectedIndex) < (aListBox.Items.Count - ((LinesDisplayed * 2))))
                  )
                )
               )
            {
                aListBox.TopIndex = aListBox.SelectedIndex;
            }
        }
0
 
LVL 1

Expert Comment

by:zonkerman
ID: 22697342
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.

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question